1

我有以下将遍历多维数组的 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;
}

您会注意到 $key 是找到的最新数组的地址。但我想拥有父数组的索引。

数组示例:

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
    )
)
4

1 回答 1

4

这应该可以帮助你。

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

将显示获取数据的“方式”。

array(3) {
  [0]=>
  int(3)
  [1]=>
  int(1)
  [2]=>
  int(4)
}
于 2012-09-13T22:38:20.597 回答