【问题标题】:php search and get parent array elements [closed]php搜索并获取父数组元素[关闭]
【发布时间】:2013-05-31 06:55:08
【问题描述】:

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

【问题讨论】:

  • 你需要使用递归。
  • 您应该指定结果值的格式...

标签: php arrays search


【解决方案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);

【讨论】:

  • 我认为您只需要查看 categoryNested 和 categorySlug 中的值
  • 我真的建议不要使用“全球”。想象一下,您正在从私有类中搜索密码数组。所有密码都将在全球范围内公开。
  • @ZsoltSzilagy 添加了一个修复 :)
  • @EaterOfCorpses 谢谢。 :)
【解决方案2】:

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

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 吗? 数组路径 - 用于递归,忽略。

返回

你会得到数组路径,或者是假的。调用

array_searchRecursive($myhaystackarray, 'myneedle');

【讨论】:

  • 不知道count嵌套数组,一定是递归的
  • @John 进行了相应的编辑。
  • @John 请不要忘记投票并接受对您有帮助的答案,以帮助有类似问题的人访问该网站。
猜你喜欢
  • 1970-01-01
  • 2019-09-20
  • 1970-01-01
  • 1970-01-01
  • 2016-09-18
  • 2012-12-08
  • 2013-12-28
  • 2013-03-15
  • 1970-01-01
相关资源
最近更新 更多