php多维度数字能量数组的检索

2021-03-24 02:13 jianzhan
2012/12/17 at 20:48 php多维度数字能量数组的检索 in: 后端开发程序

1 php检索多维度数字能量数组的键值

以下面事例:

$foo[1]['a']['xx'] = 'bar 1';
$foo[1]['b']['xx'] = 'bar 2';
$foo[2]['a']['bb'] = 'bar 3';
$foo[2]['a']['yy'] = 'bar 4';
$foo[3]['c']['dd'] = 'bar 3';
$foo[3]['f']['gg'] = 'bar 3';
$foo['info'][1] = 'bar 5';

假如要搜索 bar 3 如何开展搜索呢。有三个結果,而这三个結果必须,看看面的涵数:
-------------------------------------------------------------------------------------------------------------------------------
function array_search_re($needle, $haystack, $a=0, $nodes_temp=array()){
global $nodes_found;
$a++;
foreach ($haystack as $key1= $value1) {
    $nodes_temp[$a] = $key1;
    if (is_array($value1)){   
      array_search_re($needle, $value1, $a, $nodes_temp);
    }
    else if ($value1 === $needle){
      $nodes_found[] = $nodes_temp;
    }
}
return $nodes_found;
}
---------------------------------------------------------------------------------------------------------------------------------
这一涵数便可以把上边要搜索到的內容所有回到出键名来
$result = array_search_re('bar 3', $foo);

print_r($result);

輸出結果为以下:
Array ( [0] = Array ( [1] = 2 [2] = a [3] = bb )
          [1] = Array ( [1] = 3 [2] = c [3] = dd )
          [2] = Array ( [1] = 3 [2] = f [3] = gg )
        )

1 php检索多维度数字能量数组的键名

function array_search_key($needle, $haystack){
global $nodes_found;

foreach ($haystack as $key1= $value1) {
 
 if ($key1=== $needle){
 
  $nodes_found[] = $value1;
       
   }
    if (is_array($value1)){   
      array_search_key($needle, $value1);
    }
   
   
}

return $nodes_found;
}
$result = array_search_key('a', $foo);

print_r($result);

輸出結果为以下:
 

Array
(
    [0] = Array
        (
            [xx] = bar 1
        )

    [1] = Array
        (
            [bb] = bar 3
        )

    [2] = Array
        (
            [yy] = bar 4
        )

)

标识:php, 数字能量数组