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?

2 Answers 2

2
foreach($myArray[0][2] as $v) {
 echo $v."<br>\n";
}
Sign up to request clarification or add additional context in comments.

1 Comment

with php everything is simple (almost)
0
<?php
$tensArr =  $myArray[0][2];
for($i= 0 ; $i < count($tensArr); $i++)
{
    echo  $tensArr[$i]."\n" ;

}

5 Comments

to prevent running count() in each loop, a inital variable could be set: for($i=0, $count=count($tensArr); $i < $count; $i++)
@hakre , how is it different ? It does not seem to be even micro optimizing or correcting the code ? Please share your view.
@hakre , that does not help pretty much. Count function is 0(1) , see more here ... stackoverflow.com/questions/4566314/…
MO. As you were assigning $tensArr, I thought it would complete the picture a bit more.
@hakre.. yeah that way it does help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.