0

guys I am trying to do this kind of loop.

$first_ex =
              array(

                 '1st' => array(

                    '1.1' => 'value1',
                    '1.2' => 'value2'
                    // and so on...
                 )
              );
$second_ex =
              array(

                 '1st' => array(

                    '1.1' => array(
                             1.1.1 => 'value'
                             // so on...
                    )
                    '1.2' => array(
                             1.2.1 => 'value'
                             // so on...
                    )

                 )
              );

As of now I can only do array in an array, but how can I make a code that it will automatically process all of the nested arrays no matter how many nested arrays are in there.

[Note] It does not answer my question.

googol8080
  • 11
  • 6

1 Answers1

0
function processArray($array) {
  foreach ($array as $item) {
    if (is_array($item)) {
      processArray($item);
    } else {
      processValue($item);
    }
  }
}

function processValue($value) {
  echo $value;
}

processArray($second_ex);
Bar-code
  • 277
  • 1
  • 6
  • 1
    While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Scott Weldon Oct 18 '16 at 19:33
  • What I did is close with your code, I can't vote you though. – googol8080 Oct 19 '16 at 00:52
  • @googol8080. Then accept the answer, if it helped you – tom redfern Oct 19 '16 at 12:12
  • @googol8080 you use that tick mark on the side of the answer, and vote the answer up if you like – tom redfern Nov 18 '16 at 14:47