从多维数组中查找最后一个数组并获取索引

时间:2020-01-16 09:50:05

标签: php arrays multidimensional-array

我正在研究多维数组,并发现类似于this question的案例。尝试后,事实证明我必须寻求ID和唯一值,因此它与我的情况有所不同。

所以我的情况是这样的:我有一个这样的多维数组:

Array
(
    [0] => Array
        (
            [item] => null
            [count] => 0
            [child] => Array
                (
                    [Dagadu Bocah] => Array
                        (
                            [item] => Dagadu Bocah
                            [count] => 47
                            [child] => Array
                                (
                                    [HirukPikuk] => Array
                                        (
                                            [item] => HirukPikuk
                                            [count] => 5
                                            [child] => Array
                                                (
                                                    [DGD] => Array
                                                        (
                                                            [item] => DGD
                                                            [count] => 1
                                                            [child] => 
                                                        )

                                                )

                                        )

                                    [DGD] => Array
                                        (
                                            [item] => DGD
                                            [count] => 5
                                            [child] => Array
                                                (
                                                    [Malioboroman] => Array
                                                        (
                                                            [item] => Malioboroman
                                                            [count] => 1
                                                            [child] => 
                                                        )

                                                )

                                        )

                                    [Malioboroman] => Array
                                        (
                                            [item] => Malioboroman
                                            [count] => 2
                                            [child] => 
                                        )

                                )

                        )

我要寻找的是如何搜索数组,例如'DGD',它将产生所有以'DGD'结尾的索引

DGD => Dagadu Bocah->HirukPikuk,
       Dagadu Bocah;
Malioboroman=> Dagadu Bocah->DGD,
       Dagadu Bocah;

我尝试过的方法是这样的,最终结果使用implode:

public function searchRec($haystack, $needle, $pathId=Array(), $pathIndex=Array())
{
  foreach($haystack as $index => $item) {
    $pathId[] = $item['count'];
    $pathIndex[] = $index;
    if($item['item'] == $needle) {
        $returnObject = new stdClass();
        $returnObject->match = $item;   
        $returnObject->pathId = $pathId; 
        $returnObject->pathIndex = $pathIndex; 
        return $returnObject;
    }

    if(isset($item['child']) && count($item['child']>0)) {
        $result =searchRec($item['child'], $needle, $pathId, $pathIndex);
        if($result) {
            return $result;
        }
    }
}
return false;
}
$result = searchRec($Array, "Some Text2");
echo implode(" -> ", $result->pathId);

1 个答案:

答案 0 :(得分:0)

不确定这是您要寻找的东西,但这可能有助于您解决问题。

函数pathFinder()是一个递归函数,即它将遍历主题数组所需的次数。

只需设置needle,即您要查找的key,它就会返回主题数组中键的所有路径。

其用法示例:

我们有一个$arr数组,其中包含三个称为one的键,我们将使用该函数查找所有三个键的路径:

$arr = [
    ['one' => 'once', 
     'two' => ['one' => 'twice', 'two' => 'twice', ['one' => 'thrice']], 
     'three' => 'once',]
];

$needle = 'one'; // the key we're looking for
$result = pathFinder($arr, $needle); // invoke function

/**
 * function pathFinder() returns the path(s) to a key in an array.
 *
 * @param array  $arr    the subject array
 * @param string $needle the key we're looking for
 *
 * @return array            the paths leading to the key we're looking for
 */
function pathFinder(array $arr = [], $needle = ''): array
{
    static $path = '';
    static $paths = [];

    foreach ($arr as $key => $value) {
        if (is_array($value)) {
            $path .= $key . "->";
            pathFinder($value, $needle);
        } else {
            if ($key === $needle) {
                $paths[] = $path . $key; // store path
            }
        }
    }
    return $paths; // return all found paths to key $needle
}

echo '<pre>';
print_r($result);
echo '</pre>';

输出:

Array
(
    [0] => 0->one
    [1] => 0->two->one
    [2] => 0->two->0->one
)

工作demo