i have the below php search script that will traverse a multidimentional array. when $value is found it will return it, but i wish to return the address as well (considering it is only 2 levels)
我有下面的PHP搜索脚本,将遍历一个多维数组。当找到$ value时它将返回它,但我也希望返回该地址(考虑它只有2个级别)
function arr_search($array, $line, $lvl=0)
{
// Loops through each element. If element again is array, function is recalled. If not, result is echoed.
foreach($array as $key=>$value)
{
if(is_array($value))
{
arr_search($value, $line);
}else{
if(strpos($line, $value))
echo "found $key: $value\n";
// return $value; // should return array with [?],[$key],[$value]
}
}
return false;
}
you can notice that $key is the address of the latest array found. but i want to have the index of the parent array.
您可以注意到$ key是找到的最新数组的地址。但我想拥有父数组的索引。
array example:
Array
(
[0] => Array
(
[0] => string324
[1] => string234
[2] => string7567
[3] => stringw34
)
[1] => Array
(
[0] => string4563
[1] => string37
)
[2] => Array
(
[0] => string3735
[1] => string3563
[2] => string3563
[3] => string356
[4] => string356
)
)
1 个解决方案
#1
4
This should help you.
这应该对你有帮助。
<?php
function array_find_deep(array $array, $string, array &$result) {
foreach ($array as $key => $value) {
if (is_array($value)) {
$success = array_find_deep($value, $string, $result);
if ($success) {
array_unshift($result, $key);
return true;
}
} else {
if (strcmp($string, $value) == 0) {
array_unshift($result, $key);
return true;
}
}
}
return false;
}
$array = array(
1 => array(
1 => array(
1 => "a",
2 => "b",
3 => "c",
4 => "d",
5 => "e",
),
2 => array(
1 => "f",
2 => "g",
3 => "h",
4 => "i",
5 => "j",
),
3 => array(
1 => "k",
2 => "l",
3 => "m",
4 => "n",
5 => "o",
),
),
2 => array(
1 => array(
1 => "A",
2 => "B",
3 => "C",
4 => "D",
5 => "E",
),
2 => array(
1 => "F",
2 => "G",
3 => "H",
4 => "I",
5 => "J",
),
3 => array(
1 => "K",
2 => "L",
3 => "M",
4 => "N",
5 => "O",
),
),
3 => array(
1 => array(
1 => "p",
2 => "q",
3 => "r",
4 => "s",
5 => "t",
),
2 => array(
1 => "u",
2 => "v",
3 => "w",
4 => "x",
5 => "y",
),
3 => array(
1 => "z",
2 => "P",
3 => "Q",
4 => "R",
5 => "S",
),
),
);
$result = array();
$success = array_find_deep($array, 's', $result);
var_dump($result);
Will display the "way" to get to your data.
将显示获取数据的“方式”。
array(3) {
[0]=>
int(3)
[1]=>
int(1)
[2]=>
int(4)
}
#1
4
This should help you.
这应该对你有帮助。
<?php
function array_find_deep(array $array, $string, array &$result) {
foreach ($array as $key => $value) {
if (is_array($value)) {
$success = array_find_deep($value, $string, $result);
if ($success) {
array_unshift($result, $key);
return true;
}
} else {
if (strcmp($string, $value) == 0) {
array_unshift($result, $key);
return true;
}
}
}
return false;
}
$array = array(
1 => array(
1 => array(
1 => "a",
2 => "b",
3 => "c",
4 => "d",
5 => "e",
),
2 => array(
1 => "f",
2 => "g",
3 => "h",
4 => "i",
5 => "j",
),
3 => array(
1 => "k",
2 => "l",
3 => "m",
4 => "n",
5 => "o",
),
),
2 => array(
1 => array(
1 => "A",
2 => "B",
3 => "C",
4 => "D",
5 => "E",
),
2 => array(
1 => "F",
2 => "G",
3 => "H",
4 => "I",
5 => "J",
),
3 => array(
1 => "K",
2 => "L",
3 => "M",
4 => "N",
5 => "O",
),
),
3 => array(
1 => array(
1 => "p",
2 => "q",
3 => "r",
4 => "s",
5 => "t",
),
2 => array(
1 => "u",
2 => "v",
3 => "w",
4 => "x",
5 => "y",
),
3 => array(
1 => "z",
2 => "P",
3 => "Q",
4 => "R",
5 => "S",
),
),
);
$result = array();
$success = array_find_deep($array, 's', $result);
var_dump($result);
Will display the "way" to get to your data.
将显示获取数据的“方式”。
array(3) {
[0]=>
int(3)
[1]=>
int(1)
[2]=>
int(4)
}