如下面例子:
$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 怎么進(jìn)行查找呢。有三個(gè)結(jié)果,而這三個(gè)結(jié)果都要,看下面的函數(shù):
-------------------------------------------------------------------------------------------------------------------------------
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;
}
---------------------------------------------------------------------------------------------------------------------------------
這個(gè)函數(shù)就可以把上面要查找到的內(nèi)容全部返回出鍵名來
$result = array_search_re('bar 3', $foo);
print_r($result);
輸出結(jié)果為如下:
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搜索多維數(shù)組的鍵名
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);
輸出結(jié)果為如下:
?
Array
(
??? [0] => Array
??????? (
??????????? [xx] => bar 1
??????? )
??? [1] => Array
??????? (
??????????? [bb] => bar 3
??????? )
??? [2] => Array
??????? (
??????????? [yy] => bar 4
??????? )
)
]]>JS判斷一個(gè)變量是否是數(shù)組的方法
//for in循環(huán)數(shù)組
//for循環(huán)數(shù)組
有時(shí)我們要把一個(gè)全為空字符串組成的數(shù)組如:array('','','');當(dāng)成是空對待,因?yàn)槔锩娌缓魏螖?shù)據(jù)
使用empty()顯然是不行的,因?yàn)槔锩姘巳齻€(gè)值,只是這些值都是空字符串,用count()也不可以
那么可以用一種變通的方式,先把數(shù)組用implode轉(zhuǎn)換成字符串,再判斷字符串是否為真就可以了:
$a=array('','');
$a = implode('',$a);i
f($a)'為真';
else echo '為假';
]]>