473,382 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

PHP and a dynamic array

4
Hi all,

I have a number of arrays, all of which could contain different headers.

I have built an array of all the headers so that I can use the if_array_key_exists function.

The code works fine for the first level of the array, but i just cant seem to figure out how to do it for the next layer. if the gender is not there it misses it out and thats the part i cant figure out

I have included 2 array examples and my code, i just havent included the code for building the header array.

Any help would be appreciated

the arrays (notice gender is in one and not the other, plus the interested array doesnt contain the same number of entries.
Expand|Select|Wrap|Line Numbers
  1.  
  2. Array
  3. (
  4.     [firstname] => example first
  5.     [lastname] => example last
  6.     [email] => example@example.com
  7.     [gender] => Female
  8.     [dob] => Array
  9.         (
  10.             [day] => 
  11.             [month] => 
  12.             [year] => 
  13.         )
  14.  
  15.     [interested] => Array
  16.         (
  17.             [Haircare] => 1
  18.             [Hair removal] => 1
  19.             [Shaving] => 1
  20.             [Grooming] => 1
  21.         )
  22.  
  23.     [agree] => 1
  24.     [x] => 104
  25.     [y] => 17
  26. )
  27.  
  28. <br><pre>Array
  29. (
  30.     [firstname] => whiteyoh
  31.     [lastname] => example
  32.     [email] => another@hotmail.com.com
  33.     [gender] => Male
  34.     [dob] => Array
  35.         (
  36.             [day] => 16
  37.             [month] => 3
  38.             [year] => 1999
  39.         )
  40.  
  41.     [interested] => Array
  42.         (
  43.             [Hair removal] => 1
  44.             [Shaving] => 1
  45.         )
  46.  
  47.     [agree] => 1
  48.     [x] => 53
  49.     [y] => 15
  50. )
  51.  
  52.  


THE CODE

Expand|Select|Wrap|Line Numbers
  1. foreach ($headers as $header){
  2.  
  3.     $reference .= $header . $tab;
  4.  
  5. }
  6. $reference .= $cr;
  7.  
  8. $SQL = "SELECT converted_text FROM forms_subscribed";
  9. $results = tep_db_query($SQL);
  10.  
  11. while ($row = tep_db_fetch_array($results)){
  12.  
  13.     $entry = unserialize($row['converted_text']);
  14.  
  15.     foreach ($headers as $key=> $header){
  16.  
  17.  
  18.                 if (array_key_exists($header, $entry)){
  19.                     $reference .= $entry[$header] . $tab;
  20.  
  21.                 } else {
  22.  
  23.                     $reference .= " " . $tab;
  24.  
  25.  
  26.                 }
  27.  
  28.  
  29.  
  30.  
  31.     $reference .= $cr;
  32. }
  33.  
Aug 22 '11 #1

✓ answered by dlite922

inside the if statement where you check if array_key_exists, check whether that the value is an array itself by using is_array() like so

Expand|Select|Wrap|Line Numbers
  1.  
  2. if (array_key_exists($header, $entry)){
  3.     if(is_array($entry[$header])) {
  4.         foreach($entry[$header] as $entry2) {
  5.             // DO SOMETHING WITH D.O.B. and INTERESTED ARRAYS
  6.         }
  7.     } else
  8.     {
  9.         $reference .= $entry[$header] . $tab;
  10.     }
  11. } else 
  12. {
  13.     $reference .= " " . $tab;
  14. }
  15.  
  16.  
Only question remains, how do you know what headers to put for DOB if your $header's array is one dimensional?

I'm going to tell you that solution then tell you the correct way of going about this whole thing.

create a for loop with an $index instead of a foreach. Inside that second foreach loop increase the $index by one, assuming your header array is flat like this:

array(...'dob','day','mon','year','interested','Ha irCare') etc...

as you can see it get's very messy.

if you want this in a flat file (tab separated file), then obviously the data needs to be flat. You need to think about how to flatten your data array so that there are no secondary levels.

Look in the comment section here: http://php.net/array_values

Couple of examples of how to flatten a multi-dim array.

Cheers,


Dan

2 2380
dlite922
1,584 Expert 1GB
inside the if statement where you check if array_key_exists, check whether that the value is an array itself by using is_array() like so

Expand|Select|Wrap|Line Numbers
  1.  
  2. if (array_key_exists($header, $entry)){
  3.     if(is_array($entry[$header])) {
  4.         foreach($entry[$header] as $entry2) {
  5.             // DO SOMETHING WITH D.O.B. and INTERESTED ARRAYS
  6.         }
  7.     } else
  8.     {
  9.         $reference .= $entry[$header] . $tab;
  10.     }
  11. } else 
  12. {
  13.     $reference .= " " . $tab;
  14. }
  15.  
  16.  
Only question remains, how do you know what headers to put for DOB if your $header's array is one dimensional?

I'm going to tell you that solution then tell you the correct way of going about this whole thing.

create a for loop with an $index instead of a foreach. Inside that second foreach loop increase the $index by one, assuming your header array is flat like this:

array(...'dob','day','mon','year','interested','Ha irCare') etc...

as you can see it get's very messy.

if you want this in a flat file (tab separated file), then obviously the data needs to be flat. You need to think about how to flatten your data array so that there are no secondary levels.

Look in the comment section here: http://php.net/array_values

Couple of examples of how to flatten a multi-dim array.

Cheers,


Dan
Aug 22 '11 #2
paulmc
4
Hi Dan thanks for the reply.

After posting this I did start thinking about the fact that it was CSV and would need to be flat. What i did was when I unserialized from the database i pulled anything from an array and moved it down the the first tier, then just looped through everything that was not an array, which worked.

I will work through what you have suggested and see what I can get with that as well.


Thanks again for replying.

Regards

Paul
Aug 23 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Vasileios Zografos | last post by:
Hello, I have a function that generates some values (e.g. vertices in 2d space) the number of which I dont know. So, it could generate 20 vertices, 100 vertices, or even 1 vertex. void...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
1
by: lemonade | last post by:
Hello! Can someone explain to me the difference between dynamic array of pointers vs dynamic array of objects by giving a real life example. Following is the code that I am using for dynamic...
6
by: KeithJ | last post by:
Hello, I am fairly new to VB6 and I am experiencing a problem with a dynamic array. I made a program that calculates Body Mass Index and saves each individual BMI number to a sequential disk...
19
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read....
1
by: Gurur | last post by:
Hi all, I have a doubt. If I have 2 structures and one is parent of other , ie the child structure is present in the parent one . And if the child structure is declared as dynamic array in the...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
13
by: kwikius | last post by:
Does anyone know what a C99 dynamic array is, and if it will be useable in C++? regards Andy Little
1
by: lumumba401 | last post by:
Hello everybody, i am asking about how to define a bidimensional dynamic array as a global variable to use as incoming variable in a function Let us see , for example in a part of a programm...
1
by: remya1000 | last post by:
i'm using VB.net 2003 application program. i'm trying to convert a VB6 program to VB.NET. The VB6 code i'm trying to convert is shown below. declared g_Share() array in module and trying to add...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.