我使用此函数在嵌套数组中进行搜索,但该数组始终为null:
$arr3 = [
'first' => 1,
'second' => 2,
'third' => [
'fourth' => 4,
]
];
/**
* returns the key for the first found value
*
* @param $needle
* @param array $haystack
* @return false|int|string
*/
function array_search_value($needle, array $haystack) {
$result = null;
$found = array_search($needle, $haystack);
if ( $found !== false ) {
// end the recursion
$result = $found;
} else {
foreach ($haystack as $key => $item) {
if (is_array($item)) {
array_search_value($needle, $item);
} else {
continue;
}
}
}
return $result;
}
var_dump(array_search_value(4, $arr3));
我不知道我做错了什么?
var_dump()结果应为string "fourth"
。
答案 0 :(得分:3)
如果您在递归过程中找到所需的内容,则实际上并没有将其存储在任何地方。这是我推荐的方法:
$arr3 = [
'first' => 1,
'second' => 2,
'third' => [
'fourth' => 4,
]
];
/**
* returns the key for the first found value
*
* @param $needle
* @param array $haystack
* @return null|array
*/
function array_search_value($needle, array $haystack) {
$result = null;
$found = array_search($needle, $haystack);
if ( $found !== false ) {
// end the recursion
$result = [ $found ]; //Array will make sense in a bit
} else {
foreach ($haystack as $key => $item) {
if (is_array($item)) {
$found = array_search_value($needle, $item);
if ($found !== null) {
return array_merge([$key],$found);
}
} else {
continue;
}
}
}
return $result;
}
var_dump(array_search_value(4, $arr3));
返回数组的原因是,如果子数组与主数组具有相同的键,那么您可以通过递归访问返回的每个数组条目的数组索引来一致地检索正确的键。
在以下位置签出代码:http://sandbox.onlinephpfunctions.com/code/085c949f660504010ed7ebb7a846e31b3a766d61
下面是一个示例,说明为什么可能需要返回数组:
如果考虑数组:
$arr3 = [
'a' => 1,
'b' => 2,
'c' => [
'a' => 4,
],
"d"=>[
"a" => [
"a" => 19
]
]
];
如果您要查找4并且不返回数组,则会返回a
,但这也将是模棱两可的,因为a
的根数组中包含1
http://sandbox.onlinephpfunctions.com/code/43c2f2dfa197400df1e5748e12f12e5346abed3e
如果不止一个,您可以修改上面的内容以获取所有导致给定结果的路径。
function array_search_value_all($needle, array $haystack) {
$result = [];
$found = array_search($needle, $haystack);
if ( $found !== false ) {
// end the recursion
$result[] = [ $found ]; //Array will make sense in a bit
} else {
foreach ($haystack as $key => $item) {
if (is_array($item)) {
$found = array_search_value($needle, $item);
if ($found !== []) {
$result[] = array_merge([$key],$found);
}
} else {
continue;
}
}
}
return $result;
}
array_search_value_all
将返回导致该值的所有路径的数组。
示例:http://sandbox.onlinephpfunctions.com/code/fa4f5274703abb221f171c6e3ace5529594cdc8c
答案 1 :(得分:2)
您缺少将递归调用分配给您$result
的权限。您需要更改:
if (is_array($item)) {
array_search_value($needle, $item);
收件人:(请注意,找不到的值会不断搜索,而不仅仅是返回)
if (is_array($item) ) {
$i = array_search_value($needle, $item);
if ($i)
return $i;
答案 2 :(得分:1)
只需返回array_search_value
函数的递归调用:
$arr3 = [
'first' => 1,
'second' => 2,
'third' => [
'fourth' => 4,
]
];
/**
* returns the key for the first found value
*
* @param $needle
* @param array $haystack
* @return false|int|string
*/
function array_search_value($needle, array $haystack) {
$result = null;
$found = array_search($needle, $haystack);
if ( $found !== false ) {
// end the recursion
$result = $found;
} else {
foreach ($haystack as $key => $item) {
if (is_array($item)) {
return array_search_value($needle, $item);
} else {
continue;
}
}
}
return $result;
}
var_dump(array_search_value(4, $arr3));
答案 3 :(得分:1)
在递归调用函数时,必须确保将递归调用的返回值传播回原始调用者:
$arr3 = [
'first' => 1,
'second' => 2,
'third' => [
'fourth' => 4,
]
];
/**
* returns the key for the first found value
*
* @param $needle
* @param array $haystack
* @return false|int|string
*/
function array_search_value($needle, array $haystack) {
$result = null;
$found = array_search($needle, $haystack);
if ( $found !== false ) {
// end the recursion
$result = $found;
} else {
foreach ($haystack as $key => $item) {
if (is_array($item)) {
$target = array_search_value($needle, $item);
if ($target){
return $target;
}
} else {
continue;
}
}
}
return $result;
}
var_dump(array_search_value(4, $arr3));
答案 4 :(得分:0)
$arr3 = [
'first' => 1,
'second' => 2,
'third' => [
'fourth' => 4,
]
];
/**
* returns the key for the first found value
*
* @param $needle
* @param array $haystack
* @return false|int|string
*/
function array_search_value($needle, array $haystack) {
$found = array_search($needle, $haystack);
if ($found) {
// put directly return here
return $found;
} else {
foreach ($haystack as $key => $item) {
if (is_array($item)) {
return array_search_value($needle, $item);
} else {
continue;
}
}
}
}