php - 给定子索引时返回 PHP 数组父索引

标签 php

我一直在努力编写一些优雅的代码,这些代码将在给定子索引时返回 PHP 数组父索引。也许这最好通过例子来说明。下面我们有一个多维数组:

$array = array(
    'Level 1a' => array(
        'Level 2a' => array(
            'Level 3a' => array(
                'Level 4a' => array(

                )
            )
        ),
    ),
    'Level 1b' => array(
        'Level 2b' => array(
            'Level 3b' => array(
                'Level 4b' => array(
                    'Level 5b' => array()
                )
            )
        ),
    ),
    'Level 1c' => array(
        'Level 2c' => array(
            'Level 3c' => array(
                'Level 4c' => array(

                )
            )
        ),
        'Level 2d' => array(
            'Level 3e' => array(
                'Level 4e' => array(

                )
            )
        ),
        'Level 2f' => array(
            'Level 3g' => array(
                'Level 4h' => array(

                )
            )
        ),
    ),
);

现在我想得到一个索引,例如 Level 4c 并让它返回它的父级:

$return = array(
    'Level 1c' => array(
        'Level 2c' => array(
            'Level 3c' => array(
                'Level 4c' => array(

                )
            )
        )
    )
);

到目前为止我想出的最好的是以下函数,它将以字符串形式返回它,有点......

function getIndexesParents($needle,$haystack,$parentKey=false,$debug=''){

    foreach($haystack as $index => $child){

        echo '@'.$index.' parentKey is "'.$parentKey.'"<br/>';
        if( $parentKey == false ){
            $parentKey = $index;
            echo 'New parentKey is "'.$parentKey.'"<br/>';
        }
        else{
            $parentKey.= '->'.$index;
        }

        if( $index == $needle ){
            return $parentKey;
        }
        else if( is_array($child) && count($child) > 0){
            echo "Going into next array under parentKey '$parentKey'<br/>";
            $result = getIndexesParents($needle,$child,$parentKey,'recursion');
            if( $result == false ){
                $parentKey = false;
            }
            else{
                return $result;
            }
        }
        else{
            return false;
        }
    }
}

这将返回“Level 1c->Level 2c->Level 3c->Level 4c”我正在构建这个来破解一个解决方案,但坦率地说,代码很难看且难以阅读。那么有人可以解决这个问题吗?

最佳答案

这使用与您生成字符串的逻辑基本相同的逻辑,但我认为它更紧凑一些。我想,它是否丑陋和难以阅读取决于旁观者的眼睛。

function get_path($array, $target, $path = "") {
   if (is_array($array)) {
      foreach ($array as $key => $value) {
         if ($key == $target) {
            return $path ? "$path->$key" : $key;
         }
         $branch = get_path($value, $target, $path ? "$path->$key" : $key);
         if ($branch) {
            return $branch;
         }
      }
   }
};

我最初回答时误解了这一点;我以为你只是想用一种更短的方法来获取路径的字符串表示形式。这应该返回实际的数组路径(如果未找到目标键,则返回 null)。

function get_path($subject, $target) {
    if (is_array($subject)) {
        if (isset($subject[$target])) return $subject;
        else {
            foreach ($subject as $key => $value) {
                $result = get_path($value, $target);
                if ($result) return [$key => $result];
            }
        }       
    }
}

关于php - 给定子索引时返回 PHP 数组父索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25004229/

相关文章:

php - 我服务器上每个文件夹的不同 PHP 版本

php - 现在切换到 SSL PHP session 需要两次尝试

PHP:不等于 (!=) 运算符的多个条件不起作用

php - 如果条件与范围

php - MYSQL 使用数学更新数据库?

mysqli - undefined variable : mysqli - PHP OOP

PHP 不会将数据插入 Mysql 数据库 - 不会引发错误

php - Laravel 通过 excel 上传图片

php - 混合不同类别的结果,在 MySQL 中按分数排序

java - 尝试上传图像时,base64_decode 返回 null