Results 1 to 3 of 3

Thread: PHP Loop through arrays within an array

  1. #1
    Join Date
    Aug 2006
    Location
    Atlanta, GA
    Beans
    34
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    PHP Loop through arrays within an array

    Ok, going crosseyed here so I figured you smart folks could point me in the right direction. I have an array with 4 keys where the value associated with each key is an array (all of those arrays are the same length)

    The array looks similar to this with a var_dump()

    array(2) {
    ["Key 1"] => array (3) {
    [0] => string "value1"
    [1] => string "value2"
    [3] => string "value3"
    }
    {
    ["Key 2"] => array (3) {
    [0] => string "value4"
    [1] => string "value5"
    [3] => string "value6"
    }
    }

    I'd like the table to look like this

    Key 1 Key 2
    value1 value4
    value2 value5
    value3 value6

    does that make sense? I can't figure out how to loop through and get table to come out correct

  2. #2
    Join Date
    Oct 2006
    Location
    Països Catalans
    Beans
    366
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: PHP Loop through arrays within an array

    There might be a better way (I haven't used PHP for some time now), but this should work:

    Code:
    <?PHP
    
    $array = array(
    		'Key 1' => array(
    				'Value 1',
    				'Value 2',
    				'Value 3',
    			),
    		'Key 2' => array(
    				'Value 4',
    				'Value 5',
    				'Value 6',
    			),
    	);
    
    print '<table width="300px"><tr>';
    $num_elements = 0;
    foreach($array AS $key => $value) {
    	print '<td><b>'.$key.'</b></td>';
    	if (count($value) > $num_elements) {
    		$num_elements = count($value);
    	}
    }
    print '</tr>';
    for($i = 0; $i < $num_elements; $i++) {
    	print '<tr>';
    	foreach($array AS $value) {
    		print '<td>'.$value[$i].'</td>';
    	}
    	print '</tr>';
    }
    print '</table>';
    
    ?>
    Siegfried-A. Gevatter Pujals
    Ubuntu Developer. Debian Developer. Zeitgeist Core Developer.

  3. #3
    Join Date
    Aug 2006
    Location
    Atlanta, GA
    Beans
    34
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: PHP Loop through arrays within an array

    Quote Originally Posted by RainCT View Post
    There might be a better way (I haven't used PHP for some time now), but this should work:
    Thanks RainCT. I'll will give that a shot and see where it gets me

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •