PHP and a dynamic array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • paulmc
    New Member
    • Aug 2011
    • 4

    PHP and a dynamic array

    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_ex ists 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.
    Code:
    Array
    (
        [firstname] => example first
        [lastname] => example last
        [email] => example@example.com
        [gender] => Female
        [dob] => Array
            (
                [day] => 
                [month] => 
                [year] => 
            )
    
        [interested] => Array
            (
                [Haircare] => 1
                [Hair removal] => 1
                [Shaving] => 1
                [Grooming] => 1
            )
    
        [agree] => 1
        [x] => 104
        [y] => 17
    )
    
    <br><pre>Array
    (
        [firstname] => whiteyoh
        [lastname] => example
        [email] => another@hotmail.com.com
        [gender] => Male
        [dob] => Array
            (
                [day] => 16
                [month] => 3
                [year] => 1999
            )
    
        [interested] => Array
            (
                [Hair removal] => 1
                [Shaving] => 1
            )
    
        [agree] => 1
        [x] => 53
        [y] => 15
    )


    THE CODE

    Code:
    foreach ($headers as $header){
    	
    	$reference .= $header . $tab;
    	
    }
    $reference .= $cr;
    
    $SQL = "SELECT converted_text FROM forms_subscribed";
    $results = tep_db_query($SQL);
    
    while ($row = tep_db_fetch_array($results)){
    	
    	$entry = unserialize($row['converted_text']);
    	
    	foreach ($headers as $key=> $header){
    
    		
    				if (array_key_exists($header, $entry)){
    					$reference .= $entry[$header] . $tab;
    				
    				} else {
    					
    					$reference .= " " . $tab;
    					
    					
    				}
    				
    				
    
    	
    	$reference .= $cr;
    }
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

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

    Code:
    if (array_key_exists($header, $entry)){
        if(is_array($entry[$header])) {
            foreach($entry[$header] as $entry2) {
                // DO SOMETHING WITH D.O.B. and INTERESTED ARRAYS
            }
        } else
        {
            $reference .= $entry[$header] . $tab;
        }
    } else 
    {
        $reference .= " " . $tab;
    }
    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','ye ar','interested ','HairCare') 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

    Comment

    • paulmc
      New Member
      • Aug 2011
      • 4

      #3
      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

      Comment

      Working...