views:

58

answers:

4

Hi all, This is a basic looping question but with a twist, so it's likely that i'm missing something easy - apologies in advance...

I'm trying to pull the results from an array $testoutput - which is filled with 3 arrays:

Running the following code:

foreach ($testoutput as $ID => $Array) {
   echo $Array . "<BR>";
}

Returns:

ARRAY
ARRAY
ARRAY

Adding a second nested loop with the following code:

foreach ($testoutput as $ID => $Array) {
   foreach ($Array as $ID => $L1item) {
      echo $L1item . "<BR>";
   }
}

Results in:

String1a
String1b
String1c
ARRAY
String2a
String2b
String2c
ARRAY
String3a
String3b
String3c
ARRAY

I'm fine with retuning all of the above strings, however, I can't figure out how to return the values from the 3rd-level of nested Arrays.

Is there an easy way to do this?

Many thanks in advance.

+1  A: 

You can use array_map

$testoutput = array('x', array('y', 'z', array('1', '2', '3')));
function output($element) {
    if(is_array($element)) {
       array_map('output', $element); //RECURSION
       return;
    }
    echo $element;
}
array_map('output', $testoutput);   

Or if you prefer, you can use array_walk_recursive:

function output(&$value, $index) {
    echo $value;
}
array_walk_recursive($testoutput, 'output');
Jacob Relkin
This doesn't appear to solve his nested array problem.
ArtBIT
@ArtBIT, How is that?
Jacob Relkin
Sorry, I commented on your code before you edited it. The updated version is correct.
ArtBIT
@ArtBIT, I edited it like 20 minutes ago, haha.
Jacob Relkin
Not true: http://cl.ly/ad4a1bf86ce85c39aff4 - You edited it 3minutes after my comment.
ArtBIT
@ArtBIT, *look* at the edit. http://stackoverflow.com/posts/3993346/revisions
Jacob Relkin
I blame the slacky servers then, since I didn't see any updates ;) Sorry
ArtBIT
A: 

Try this:

/** 
 * array nested_array_map(callback $callback, array $array)
 * Warning - doesn't check for recursion, 
 *           therefore child arrays shouldn't contain references to any of parent level arrays
 *
 * @param $callback, function
 * @param $array, array of elements to map the function to
 * @return array
 */
function nested_array_map($callback, $param) {
    if (!is_array($param)) {
        return call_user_func($callback, $param);
    }

    $result = array();
    foreach ($param as $index => $value) {
        $result[$index] = nested_array_map($callback, $value);
    }
    return $result;
}

function echo_value($value) {
    echo "$value\n";
    return $value;
}

$test = array(
    '1st level'
    ,array(
        '2nd level'
        ,array(
            '3rd level'
        )
        ,'2nd level'
    )
    ,array(
        '2nd level'
    )
    ,'1st level'
);

$result = nested_array_map('echo_value', $test);
ArtBIT
+1  A: 
foreach ($testoutput as $key1 => $value1) {
   foreach ($value1 as $key2 => $value2) {
      if(is_array($value2))
      {
              foreach ($value2 as $key3 => $value3) {
                          echo $value3;
              }
      }
      else
      {
              echo $value2;
      }
   }
}
Maulik Vora
A: 

Thanks guys - all three methods do the trick - quick follow-up question:

Using the above methods, how do I return the results for a value matching a specific key within the nested array (the one on the third level)?

Thanks again!

DJVerdeva
Also wasn't sure how to leave a comment or thank-you any other way, so sorry about this...
DJVerdeva