-1

我有 assoc php 数组

0 => array
    (
        'categoryName' => 'Moto'
        'categoryTitle' => 'Moto'
        'categorySlug' => 'moto-and-tech'
        'categoryAttr' => array
        (
            0 => 'test1'
            1 => 'test2'
            2 => 'test3'
            3 => 'test4'
        )
        'categoryNested' => array
        (
            0 => array
            (
                'categoryName' => 'anything'
                'categoryTitle' => 'anything'
                'categorySlug' => 'anything'
                'categoryAttr' => array
                (
                    0 => 'test1'
                    1 => 'test1'
                    2 => 'test1'
                    3 => 'test1'
                )
                'categoryNested' => array()
            )
            1 => array
            (
                'categoryName' => 'any'
                'categoryTitle' => 'any'
                'categorySlug' => 'any'
                'categoryAttr' => array
                (
                    0 => 'test1'
                    1 => 'test1'
                    2 => 'test1'
                    3 => 'test1'
                )
                'categoryNested' => array()
            )

我如何搜索插入的数组key(catgorySlug),并返回所有父元素 categoryName ?

4

2 回答 2

1

这是这样的递归函数,希望对您有所帮助:

$array = array
(
    'categoryName' => 'Moto',
    'categoryTitle' => 'Moto',
    'categorySlug' => 'moto-and-tech',
    'categoryAttr' => array
    (
        0 => 'test1',
        1 => 'test2',
        2 => 'test3',
        3 => 'test4'
    ),
    'categoryNested' => array
    (
        0 => array
        (
            'categoryName' => 'anything',
            'categoryTitle' => 'anything',
            'categorySlug' => 'anything',
            'categoryAttr' => array
            (
                0 => 'test1',
                1 => 'test1',
                2 => 'test1',
                3 => 'test1'
            ),
            'categoryNested' => array()
        ),
        1 => array
        (
            'categoryName' => 'any',
            'categoryTitle' => 'any',
            'categorySlug' => 'any',
            'categoryAttr' => array
            (
                0 => 'test1',
                1 => 'test1',
                2 => 'test1',
                3 => 'test1'
            ),
            'categoryNested' => array()
        )
    )
);

function findByKey($key,$tmp) {
    $results = array();
    foreach($tmp as $k=>$v) {
        if ($k===$key && !is_array($v)) {
           $results[]=$v;
        }
        if (is_array($v)) {
           $results = array_merge($results,findByKey($key,$v));
        }
    }
    return $results;
}

$results = findByKey('categoryTitle',$array);

var_dump($results);
于 2013-05-31T07:52:13.573 回答
1

对于未知的数组深度,使用递归

function array_searchRecursive( $needle, $haystack, $strict=false, $path=array() )
{

    foreach( $haystack as $key => $val ) {
        if( is_array($val) && $subPath = array_searchRecursive($needle, $val, $strict, $path) ) {
            $path = array_merge($path, array($key), $subPath);
            return $path;
        } elseif( (!$strict && $val == $needle) || ($strict && $val === $needle) ) {
            $path[] = $key;
            return $path;
        }
    }
    return false;
}

参数: 混合针 - 你搜索的数组 haystack - 搜索对象 bool 严格 - 接受“1”为 1 ?数组路径 - 用于递归,忽略。

返回

您将获得数组路径或 false。打电话给

array_searchRecursive($myhaystackarray, 'myneedle');
于 2013-05-31T07:19:40.947 回答