-2

I want to create a function that for a random array will search for a given key and will return all values for that key in an array. Here is what I have written so far...

$testArray = array (
    'subArray1' => array(
        'key1' => "Sub array 1 value 1",
        'key2' => "Sub array 1 value 2",
        'key3' => array(
            'subkey1' => array(
                'subsubkey' => 'sub sub sub value',
            ),
        ),
    ),
    'subArray2' => array(
        'key1' => "Sub array 2 value 1",
        'key2' => "Sub array 2 value 2",
        'key3' => array(
            'subkey1' => array(
                'subsubkey' => 'sub sub sub value 2',
            ),
        ),
    ),
);




function recursiveSearch($testArray,$key,$results)
{
   //    var_dump($testArray);
    if(is_array($testArray)){
        foreach($testArray as $k => $v){
           if($k == $key){
                return $v;
           }
            else if(is_array($v)){
                array_push($results, recursiveSearch($v,$key,$results));
           }
       }
   } 
    else{
       return ;
   }
   return $results;
}

$empt = array();

$output = recursiveSearch($testArray,'subsubkey',$empt);


var_dump($output);

This the output I get at the moment... I want to get a simple array with the values in it.

array(2) {
  [0]=>
  array(1) {
    [0]=>
    array(1) {
      [0]=>
      string(17) "sub sub sub value"
    }
  }
  [1]=>
  array(2) {
    [0]=>
    array(1) {
      [0]=>
      array(1) {
        [0]=>
        string(17) "sub sub sub value"
      }
    }
    [1]=>
    array(2) {
      [0]=>
      array(1) {
        [0]=>
        array(1) {
          [0]=>
          string(17) "sub sub sub value"
        }
      }
      [1]=>
      string(19) "sub sub sub value 2"
    }
  }
}

I am not really sure why the result array is like this ...

The format for the result array that I want is :

['sub sub sub value','sub sub sub value 2']

Cœur
  • 37,241
  • 25
  • 195
  • 267
orestiss
  • 2,183
  • 2
  • 19
  • 23
  • I see an "I want" but no question. You already have code, what have you tried to change it, where do you think the improvement needs to be done? – kero Nov 03 '15 at 10:49

2 Answers2

1

An improved version of xPheRe

/**
 * Recursive find in the given $array and $needle.
 * @param $array The input array.
 * @param $needle The needle to find.
 * @return Returns all the values find by the given key.
 */
function recursiveFind(array $array, $needle)
{
    $iterator  = new RecursiveArrayIterator($array);
    $recursive = new RecursiveIteratorIterator($iterator,
                         RecursiveIteratorIterator::SELF_FIRST);
    $results = array();
    foreach ($recursive as $key => $value) {
        if ($key === $needle) {
            array_push($results, $value);
        }
    }
    return $results;
}

Usage

$output = recursiveFind($testArray, 'subsubkey');
var_dump($output);

Output

array(2) {
  [0]=>
  string(17) "sub sub sub value"
  [1]=>
  string(19) "sub sub sub value 2"
}
Community
  • 1
  • 1
Davide Pastore
  • 8,678
  • 10
  • 39
  • 53
0

The updated function of my version is as follows:

function recursiveSearch($testArray,$key){
    $empt = array();
    function recurSearch($testArray,$key,$results)
    {
        if(is_array($testArray)){
            foreach($testArray as $k => $v){
                if($k === $key){
                    array_push($results,$v);
                }
                else if(is_array($v)){
                    $temp = array();
                    $tempres =  recurSearch($v,$key,$temp);
                    $results = array_merge($results,$tempres);
                }
            }
        } 
        return $results;
    }
    $res = recurSearch($testArray,$key,$empt);
    return $res;
}

seems to work : )

orestiss
  • 2,183
  • 2
  • 19
  • 23