PHP - 查找数组的父键

发布于 2024-08-26 11:21:16 字数 1197 浏览 3 评论 0原文

我正在尝试找到一种方法来返回数组父键的值。

例如,从下面的数组中我想找出父级的键,其中 $array['id'] == "0002"。 父键很明显,因为它是在这里定义的(它将是“产品”),但通常它是动态的,因此出现了问题。但“id”和“id”的值是已知的。

    [0] => Array
        (
            [data] => 
            [id] => 0000
            [name] => Swirl
            [categories] => Array
                (
                    [0] => Array
                        (
                            [id] => 0001
                            [name] => Whirl
                            [products] => Array 
                               (
                                    [0] => Array
                                        (
                                            [id] => 0002
                                            [filename] => 1.jpg
                                         )
                                    [1] => Array
                                        (
                                            [id] => 0003
                                            [filename] => 2.jpg
                                         )
                                )
                         )
                 )
          )

I'm trying to find a way to return the value of an array's parent key.

For example, from the array below I'd like to find out the parent's key where $array['id'] == "0002".
The parent key is obvious because it's defined here (it would be 'products'), but normally it'd be dynamic, hence the problem. The 'id' and value of 'id' is known though.

    [0] => Array
        (
            [data] => 
            [id] => 0000
            [name] => Swirl
            [categories] => Array
                (
                    [0] => Array
                        (
                            [id] => 0001
                            [name] => Whirl
                            [products] => Array 
                               (
                                    [0] => Array
                                        (
                                            [id] => 0002
                                            [filename] => 1.jpg
                                         )
                                    [1] => Array
                                        (
                                            [id] => 0003
                                            [filename] => 2.jpg
                                         )
                                )
                         )
                 )
          )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

几味少女 2024-09-02 11:21:16

有点粗糙的递归,但它应该可以工作:

function find_parent($array, $needle, $parent = null) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $pass = $parent;
            if (is_string($key)) {
                $pass = $key;
            }
            $found = find_parent($value, $needle, $pass);
            if ($found !== false) {
                return $found;
            }
        } else if ($key === 'id' && $value === $needle) {
            return $parent;
        }
    }

    return false;
}

$parentkey = find_parent($array, '0002');

A little crude recursion, but it should work:

function find_parent($array, $needle, $parent = null) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $pass = $parent;
            if (is_string($key)) {
                $pass = $key;
            }
            $found = find_parent($value, $needle, $pass);
            if ($found !== false) {
                return $found;
            }
        } else if ($key === 'id' && $value === $needle) {
            return $parent;
        }
    }

    return false;
}

$parentkey = find_parent($array, '0002');
故事和酒 2024-09-02 11:21:16

由于您有一个 BFSDFS 可以做到。由于结构是可变的,递归解决方案会很好地工作。当您找到值时,只需返回一个哨兵,然后在调用者中返回键即可。

Since you have a tree structure either of a BFS or DFS can do it. Since the structure is variable a recursive solution would work well. Simply return a sentinel when you find the value, then return the key in the caller.

骑趴 2024-09-02 11:21:16
function array_to_xml($array, $rootElement = null, $xml = null) {

    $_xml = $xml;

    if ($_xml === null) {
        $_xml = new SimpleXMLElement($rootElement !== null ? $rootElement : '<root/>');
    }

    $has_int_key = 0;

    foreach ($array as $k => $v) {
        if (is_array($v)) {
            if(is_int($k)){
                $this->array_to_xml($v, $k, $_xml->addChild($rootElement));
            } 
            else {
                foreach($v as $key=>$value) {
                    if(is_int($key)) $has_int_key = 1;
                }

              if ($has_int_key) {
                  $this->array_to_xml($v, $k, $_xml);
              } else {
                  $this->array_to_xml($v, $k, $_xml->addChild($k));
              }
            }
        } 
        else {
            $_xml->addChild($k, $v);
        }
    }

    return $_xml->asXML();

}
function array_to_xml($array, $rootElement = null, $xml = null) {

    $_xml = $xml;

    if ($_xml === null) {
        $_xml = new SimpleXMLElement($rootElement !== null ? $rootElement : '<root/>');
    }

    $has_int_key = 0;

    foreach ($array as $k => $v) {
        if (is_array($v)) {
            if(is_int($k)){
                $this->array_to_xml($v, $k, $_xml->addChild($rootElement));
            } 
            else {
                foreach($v as $key=>$value) {
                    if(is_int($key)) $has_int_key = 1;
                }

              if ($has_int_key) {
                  $this->array_to_xml($v, $k, $_xml);
              } else {
                  $this->array_to_xml($v, $k, $_xml->addChild($k));
              }
            }
        } 
        else {
            $_xml->addChild($k, $v);
        }
    }

    return $_xml->asXML();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文