首页 > 解决方案 > php获取多维数组中的特定数组

问题描述

我正在寻找从多维数组中获取数组的解决方案

例如; 这是我的数组:

Array
(
    [1] => Array
        (
            [id] => 2
            [name] => Laïla Mertens 
            [text] => Laïla Mertens 
            [parent_id] => 
            [href] => http://google.com
            [nodes] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [name] => Erwin Dubois
                            [text] => Erwin Dubois
                            [parent_id] => 2
                            [href] => http://google.com
                            [nodes] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                            [name] => Test Gebruiker
                                            [text] => Test Gebruiker
                                            [parent_id] => 1
                                            [href] => http://google.com
                                        )

                                    [1] => Array
                                        (
                                            [id] => 4
                                            [name] => Catharina
                                            [text] => Catharina
                                            [parent_id] => 1
                                            [href] => http://google.com
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 5
                            [name] => Araxanta
                            [text] => Araxanta
                            [parent_id] => 2
                            [href] => http://google.com
                            [nodes] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 15
                                            [name] => Kevin
                                            [text] => Kevin
                                            [parent_id] => 5
                                            [href] => http://google.com
                                            [nodes] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 16
                                                            [name] => Petra
                                                            [text] => Petra
                                                            [parent_id] => 15
                                                            [href] => http://google.com
                                                        )

                                                    [1] => Array
                                                        (
                                                            [id] => 17
                                                            [name] => Shannah
                                                            [text] => Shannah
                                                            [parent_id] => 15
                                                            [href] => http://google.com
                                                        )

                                                )

                                        )

                                )

                        )

                    [2] => Array
                        (
                            [id] => 6
                            [name] => Christian
                            [text] => Christian
                            [parent_id] => 2
                            [href] => http://google.com
                            [nodes] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 13
                                            [name] => Macy VanMaele
                                            [text] => Macy VanMaele
                                            [parent_id] => 6
                                            [href] => http://google.com
                                        )

                                    [1] => Array
                                        (
                                            [id] => 14
                                            [name] => April Poizat
                                            [text] => April Poizat
                                            [parent_id] => 6
                                            [href] => http://google.com
                                            [nodes] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 7
                                                            [name] => Sigrid
                                                            [text] => Sigrid
                                                            [parent_id] => 14
                                                            [href] => http://google.com
                                                        )

                                                )

                                        )

                                )

                        )

                    [3] => Array
                        (
                            [id] => 8
                            [name] => Nikita
                            [text] => Nikita
                            [parent_id] => 2
                            [href] => http://google.com
                        )

                    [4] => Array
                        (
                            [id] => 9
                            [name] => Salina Lemmens
                            [text] => Salina Lemmens
                            [parent_id] => 2
                            [href] => http://google.com
                        )

                    [5] => Array
                        (
                            [id] => 10
                            [name] => Emmily Polen
                            [text] => Emmily Polen
                            [parent_id] => 2
                            [href] => http://google.com
                        )

                    [6] => Array
                        (
                            [id] => 11
                            [name] => Jitka Symus
                            [text] => Jitka Symus
                            [parent_id] => 2
                            [href] => http://google.com
                        )

                    [7] => Array
                        (
                            [id] => 12
                            [name] => Amina Gigovic
                            [text] => Amina Gigovic
                            [parent_id] => 2
                            [href] => http://google.com
                        )

                )

        )

)

我想得到 id 等于 1 的数组

所以在这个例子中, [id] => 1 我想捕获 id 1 的整个数组,因此:

Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [name] => Erwin Dubois
                            [text] => Erwin Dubois
                            [parent_id] => 2
                            [href] => http://google.com
                            [nodes] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                            [name] => Test Gebruiker
                                            [text] => Test Gebruiker
                                            [parent_id] => 1
                                            [href] => http://google.com
                                        )

                                    [1] => Array
                                        (
                                            [id] => 4
                                            [name] => Catharina
                                            [text] => Catharina
                                            [parent_id] => 1
                                            [href] => http://google.com
                                        )

                                )

                        )

php中有没有一种方法可以让我拥有类似的功能;

function get_subarray($id) {
  // Select array where id = $id
}

任何指向该方向的点都非常感谢!函数中需要它,因为我会根据其唯一 ID 解析不同的用户级别

大批:

$json = file_get_contents("https://innerbeauthe.be/cp/_data/fetch.php");
$data = json_decode($json,true);

标签: phparrays

解决方案


您可以定义一个遍历数组以搜索所需数组键的递归函数。

虽然 PHP 包含一个函数array_walk_recursive(),但它会遍历叶子节点,并且很难返回父数组。同样,使用RecursiveArrayIterator实际上使获取您想要的数组而不是叶子笔记变得复杂。相反,创建自己的并不太复杂:

// Function accepts an array and a search value
function walk_for_array(array $a, $search) {
  // Loop over the array
  foreach ($a as $k => $v) {
    // Find sub-arrays (rather than scalar values)
    if (is_array($v)) {
      // Check if there is an 'id' key and if it matches your search value
      if (isset($v['id']) && $v['id'] == $search) {                                                                                           
        // return it
        return $v; 
      }   
      else {
        // Otherwise continue drilling into the array for more arrays
        $sub = walk_for_array($v, $search);
        // Return the recursive value only if the inner call
        // was non-null
        if ($sub) return $sub;
      }   
    }   
  }
}

// Call it, searching for id == 1
$found = walk_for_array($your_big_array, 1);

print_r($found);

// Call it, searching for id == 13
$found = walk_for_array($your_big_array, 13);

print_r($found);

印刷:

Array
(
    [id] => 1
    [name] => Erwin Dubois
    [text] => Erwin Dubois
    [parent_id] => 2
    [href] => http://google.com
    [nodes] => Array
        (
            [0] => Array
                (
                    [id] => 3
                    [name] => Test Gebruiker
                    [text] => Test Gebruiker
                    [parent_id] => 1
                    [href] => http://google.com
                )

            [1] => Array
                (
                    [id] => 4
                    [name] => Catharina
                    [text] => Catharina
                    [parent_id] => 1
                    [href] => http://google.com
                )

        )

)

对于 id = 13

Array
(
    [id] => 13
    [name] => Macy VanMaele
    [text] => Macy VanMaele
    [parent_id] => 6
    [href] => http://google.com
)

推荐阅读