php搜索并获取父数组元素

时间:2013-05-31 06:55:09

标签: php arrays search

我有关联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?

2 个答案:

答案 0 :(得分: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 strict - 接受“1”表示1? 数组路径 - 用于递归,忽略。

<强>返回

您将获得数组路径或false。用

打电话
array_searchRecursive($myhaystackarray, 'myneedle');

答案 1 :(得分: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);