数组搜索多维数据库在PHP?

时间:2015-11-03 10:41:53

标签: php arrays

我想创建一个函数,对于随机数组,它将搜索给定的键,并返回数组中该键的所有值。这是我到目前为止所写的内容......

$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);

这是我目前得到的输出......我想得到一个带有值的简单数组。

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"
    }
  }
}

我不确定为什么结果数组是这样的......

我想要的结果数组的格式是:

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

2 个答案:

答案 0 :(得分:1)

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;
}

<强>用法

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

<强>输出

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

答案 1 :(得分:0)

我的版本的更新功能如下:

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;
}

似乎有效:)