Jump to content

How To Output Multidimensional Array , With For Loop ?


printJimy

Recommended Posts

Hello .

( explain me plz)How can i output an Multidimensional array with for loop(or while), not with foreach ?

example this array

$exampleMultidmArr = array (
"nested1" =>array (1,2,3,4,5),
"nested2" =>array (7,20,43,44,25),
);

Thank You.

Edited by printJimy
Link to comment
Share on other sites

You could do it with a 'for' loop or a 'foreach' loop. I think it is more a matter of preference and what you are going to do with the data. I tend to use 'for' loops.

 

for(i=0; i < count($exampleMultidmArr); i++)
{
 for(j=0; j < count($exampleMultidmArr[i]); j++)
 {
   echo $exampleMultidmArr[i][j];
 }
}

 

With this method you can do additional things with the data since you are determining the exact position of each item.

 

Hope this helps.

Link to comment
Share on other sites

Just for shiggles, here is an example using while loops:

 

$mArr = array(
0=>array(1, 2, 3, 4, 5),
1=>array(7, 20, 43, 44, 25)
);

$i = 0;
$c = count($mArr);
while($i < $c)
{
 $j=0;
 $c2 = count($mArr[$i]);
 while($j < $c2)
 {
   echo $mArr[$i][$j] . PHP_EOL;
   $j++;
 }
$i++;
}

 

But, using a foreach loop is best.

Edited by AyKay47
Link to comment
Share on other sites

Thank to all of you , i really understood issue now .

Also when we have the key of array as a string , we need to use a string also in a loop , example like this ?

is it the right ?

<?php
$mArr = array(
"a"=>array(1, "string", 3, 4, 5),
"b"=>array(7, 20, 43, 44, 25)
);




for ($i="a"; $i<= "b"; $i++)
{


for ($j=0;$j<count($mArr[$i]); $j++)
{


echo $mArr[$i][$j]." ";

}
echo"<br>";
}
?>

Link to comment
Share on other sites

neither of those two arrays have a string key in them

 

"a" and "b" are both string keys.

 

In a situation where you would need to have string keys and do not want to use a foreach loop (why not?), use a persistent naming convention for the keys so you can reference them properly inside of the loop(s). Something like this:

 

$mArr = array(
"number1"=>array(1, 2, 3, 4, 5),
"number2"=>array(7, 20, 43, 44, 25)
);

$i = 0;
$c = count($mArr);
while($i < $c)
{
 $index = "number" . ($i + 1);
 $j=0;
 $c2 = count($mArr[$index]);
 while($j < $c2)
 {
       echo $mArr[$index][$j] . PHP_EOL;
       $j++;
 }
$i++;
}

Link to comment
Share on other sites

You can use array_keys to get the keys into an enumerated array. Then use a for() loop regardless of the values or order of the original keys. Using foreach() would still be a much better solution though, IMO. Iterating over arrays is exactly what it's there for.

 

$mArr = array(
"a"=>array(1, "string", 3, 4, 5),
"b"=>array(7, 20, 43, 44, 25)
);

$keys = array_keys($mArr);
$c = count($keys);

for( $i = 0; $i < $c; $i++ ) {
print_r( $mArr[$keys[$i]] );
echo '<br>';
}

Link to comment
Share on other sites

also i have a question about how to output the key of array with the for loop .

example this is an array and this array have 2 key 1 and 2 , how to output these keys .

$vegetables=array (1=>"apples", 2=>"pear");

and the output of this array i like to be (if it is possible) :

1 apples
2 pear

without any special function like

key();

or something like this .

Thanks

Edited by printJimy
Link to comment
Share on other sites

Read up on foreach (), and you'll see. ;)

Also, key () isn't a special function in any way, it's been a part of the core PHP functionality since at least 4.x. Granted, it's not often used as foreach () is often the better alternative.

i know with foreach ,

foreach ($vegetables as $k => $v) {
 echo $k;
}

, but i want with "for" .

Link to comment
Share on other sites

Then you have to use key () as well as next (), or each ().

However, in almost every case when you want the key and the value, you'll want to use foreach (). So if this is only for training: Keep it up. Just don't try to avoid foreach () in production code, just "because".

Edited by Christian F.
Link to comment
Share on other sites

And would you knock screws in with a hammer, or do the sensible thing and use a screwdriver?

 

Ok , i said that im just learning how to use other loop in these case , but i do not use" for or while" when i work with array , because i know that foreach is constructed for that.

So i just want to know how to use other loop to manipulate with array.

Edited by printJimy
Link to comment
Share on other sites

Ok , i said that im just learning how to use other loop in these case , but i do not use" for or while" when i work with array , because i know that foreach is constructed for that.

So i just want to know how to use other loop to manipulate with array.

 

There isn't really a reason that you shouldn't be using foreach for your purpose. I think you're just being awkward.

Link to comment
Share on other sites

ok ok, I will not use never again "for or while" .

Thank to all of you , topic can be closed .

 

To say you'll never use them again is a bit much, isn't it? I don't want you to leave this forum thinking that for loops and while loops are bad form.

 

Use them for their intended purposes!

Link to comment
Share on other sites

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.