1

Say I create a recursive array with this code:

$digits = 0;
$tens = 0;
$hundreds = 0;

for($i = 0; $i <= 100; $i++)
{
    $myArray[$hundreds][$tens][$digits] = $i;

    $digits++;
    if($digits > 9)
    {
        $digits = 0;
        $tens++;
    }

    if($tens > 9)
    {
        $tens = 0;
        $hundreds++;
    }
}

how could I echo out all the data fromt the 'tens array' == 2?

To be clear, I'd be looking for these results:

20 21 22 23 24 25 26 27 28 29

since im using base 10, I could just do this:

for($i=0; $i < 10; $i++)
{
  echo $myArray[0][2][$i]
}

but what if i have no idea how many elements are in the digits array?

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
SlickRick
  • 25
  • 3
  • 8

2 Answers2

2
foreach($myArray[0][2] as $v) {
 echo $v."<br>\n";
}
dynamic
  • 46,985
  • 55
  • 154
  • 231
0
<?php
$tensArr =  $myArray[0][2];
for($i= 0 ; $i < count($tensArr); $i++)
{
    echo  $tensArr[$i]."\n" ;

}
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
  • to prevent running count() in each loop, a inital variable could be set: `for($i=0, $count=count($tensArr); $i < $count; $i++)` – hakre Jun 12 '11 at 11:07
  • @hakre , how is it different ? It does not seem to be even micro optimizing or correcting the code ? Please share your view. – DhruvPathak Jun 12 '11 at 11:09
  • @hakre , that does not help pretty much. Count function is 0(1) , see more here ... http://stackoverflow.com/questions/4566314/php-what-is-the-complexity-i-e-o1-on-of-the-function-count – DhruvPathak Jun 12 '11 at 11:20
  • MO. As you were assigning `$tensArr`, I thought it would complete the picture a bit more. – hakre Jun 12 '11 at 11:22
  • @hakre.. yeah that way it does help. – DhruvPathak Jun 12 '11 at 11:34