Jump to content

Recommended Posts

How do I echo the array below in a for each loop?  I'm trying to output this data in a table.

 

Array
(
    [u] => Array
        (
            [0] => Array
                (
                    [row_id] => 1
                    [0] => Jn
                    [1] => Bo
                    [2] => do
                    [3] => jilcom
                    [4] => hocom
                )

            [1] => Array
                (
                    [row_id] => 3
                    [0] => Jk
                    [1] => Kk
                    [2] => wd
                    [3] => jk
                    [4] => 
                )

            [2] => Array
                (
                    [row_id] => 5
                    [0] => Jn
                    [1] => Bo
                    [2] => on
                    [3] => jm
                    [4] => hm
                )

        )

    [blog] => Array
        (
            [0] => Array
                (
                    [row_id] => 3
                    [0] => dk
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                )

            [1] => Array
                (
                    [row_id] => 9
                    [0] => 2rs
                    [1] => dee
                    [2] => 
                    [3] => 
                    [4] => 
                )

        )

)


Link to comment
https://forums.phpfreaks.com/topic/228339-echo-multidimentional-arrays/
Share on other sites

$array = array(array(1, item1),
                        array(2, item2),
                        array(3, item3));

for($i ; $i < ($array length) ; $i ++){
for($j ; $j < ($array length) ; $j++){
  echo $array[$i][$j];
}
}

 

Something similar to that. I'm not sure how to get the array length, which is why i've just written $array length, but it shouldn't be too hard to figure that bit out..

Basically you want a for loop for the index part of the array, and then an inner for loop for all the things associated with that index.

 

Hope that helps

 

Denno

foreach ($array as $thing => $subarr) {
  echo "$thing: ";
  foreach ($subarr as $data) {
    echo $data['row_id'];
    echo $data[0];
  }
}

 

This should work.  I've echoed some items to show how to reference them, there is no formatting of the output.

Denno, the OP's top level array is indexed by strings rather than by numbers.  In general you should use "foreach" instead of "for" in php, unless you have a special reason to be using "for".

 

Yeah sorry bout that.. I'm not familiar with OOP stuff, so I don't really think in those terms yet..

<?php
$test = Array
(
    'u' => Array
        (
            '0' => Array
                (
                    'row_id' => '1',
                    '0' => 'Jn',
                    '1' => 'Bo',
                    '2' => 'do',
                    '3' => 'jilcom',
                    '4' => 'hocom'
                ),

            '1' => Array
                (
                    'row_id' => 3,
                    '0' => 'Jk',
                    '1' => 'Kk',
                    '2' => 'wd',
                    '3' => 'jk',
                    '4' => ''
                ),

            '2' => Array
                (
                    'row_id' => 5,
                    '0' => 'Jn',
                    '1' => 'Bo',
                    '2' => 'on',
                    '3' => 'jm',
                    '4' => 'hm'
                )

        ),

    'blog' => Array
        (
            '0' => Array
                (
                    'row_id' => 3,
                    '0' => 'dk',
                    '1' => '',
                    '2' => '',
                    '3' => '',
                    '4' => '',
                ),

            '1' => Array
                (
                    'row_id' => 9,
                    '0' => 'rs',
                    '1' => 'dee',
                    '2' => '',
                    '3' => '',
                    '4' => '',
                )

        )

);
echo "<table border=3>\n";
foreach ($test as $key => $test1){
echo "<tr><td colspan=6>$key</td></tr>\n";
$start = true;
foreach ($test1 as $key => $row){
	if($start){
		echo "<tr><td>". implode('</td><td>',array_keys($row)),"</td></tr>\n";
		$start = false;
	}
	echo "<tr><td>". implode('</td><td>',$row),"</td></tr>\n";
}
}

?>

Denno, the OP's top level array is indexed by strings rather than by numbers.  In general you should use "foreach" instead of "for" in php, unless you have a special reason to be using "for".

Is there a reason to lean on foreach vs for?  I've tried to use foreach many times and for some reason I just cannot wrap my head around it.  Is it a performance/security issue?

Denno, the OP's top level array is indexed by strings rather than by numbers.  In general you should use "foreach" instead of "for" in php, unless you have a special reason to be using "for".

Is there a reason to lean on foreach vs for?  I've tried to use foreach many times and for some reason I just cannot wrap my head around it.  Is it a performance/security issue?

 

It's more of a flexibility issue.  "for" will only work for arrays indexed by ordered integers, beginning at the expected number and with no gaps.  For example if your indexes are "0, 1, 2, 4, 5, 6, 7" because you deleted the item with index #3, then "for" will no longer work as expected.  The end result is you have to keep renumbering your arrays in order for them to work with "for".

 

But foreach works with ANY array.  It can be indexed by words, numbers with gaps, mixed numbers and words, anything.  There's no need to renumber arrays after removing elements because foreach will skip them automatically.

 

The only situation I use "for" is if I have a special reason to use ordered integer indexes in my arrays, and it's important that those indexes match up.  For example if my array is storing objects at coordinates on a grid, and the indexes are the X and Y coordinates, then "for" will work better than "foreach".

 

The other good thing about foreach is that it automatically handles the situation where your array has variable size.  With "for" you need to know how long your array is, eg "for ($i = 0; $i < $array_length; $i++)", vs "foreach ($array as $item)".

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.