PHP - Get all values with specific array key from multidimensional array with unknown depth

rflrkn

I'm currently trying to get all values with a certain key from a multidimensional array with varying depth.

Sadly I'm not really good at dealing with recursion, so I don't really know how to solve this problem on my own. Here's a sample array:

Array
(
    [id] => 243
    [children] => Array
        (
            [0] => Array
                (
                    [id] => 244
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 245
                                )

                            [1] => Array
                                (
                                    [id] => 246
                                )

                        )

                )

            [1] => Array
                (
                    [id] => 249
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 250
                                )

                        )

                )

            [2] => Array
                (
                    [id] => 253
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 256
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 257
                                                )

                                        )

                                )

                        )

                )

        )

)

I basically just need a simple array with all the ID's from the multidimensional array. In this case the needed result would be:

Array
(
    [0] => 243
    [1] => 244
    [2] => 245
    [3] => 246
    [4] => 249
    [5] => 250
    [6] => 253
    [7] => 256
    [8] => 257
)

I hope somebody can help here.

Rahul

Remember this forever, keys/indexes must be unique and will be unique always.
Further, you can use array_walk_recursive to fetch ids from recursive array.

array_walk_recursive($arr, function($v,$k) use(&$result){
    if($k == 'id') $result[] = $v;
});

array_walk_recursive — Apply a user function recursively to every member of an array

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

PHP - search for key in multidimensional array and get its values deeper from the structure

分類Dev

How to parse json to get all values of a specific key within an array?

分類Dev

PHP Multidimensional Array Searching (Find key by specific value)

分類Dev

PHP How to search multidimensional array for a key from another multidimensional array remembering parent key

分類Dev

Built-in method to get flat array from a multidimensional array in PHP

分類Dev

How to get a json object with specific key values, from a json array column?

分類Dev

How to count all values in a multidimensional array?

分類Dev

Remove specific keys from multidimensional array recursively

分類Dev

Find Common most values in multidimensional array in PHP

分類Dev

PHP multidimensional array sort matching values

分類Dev

Parsing array inside object with unknown key and unknown values

分類Dev

get key value from php array with string as key

分類Dev

continue key for multidimensional array

分類Dev

PHP - How to get the sum of a multidimensional array?

分類Dev

Remove specific values from an array

分類Dev

PHP function not returning a value from multidimensional array

分類Dev

PHP extracting data from multidimensional array / JSON

分類Dev

How to get common values from 4 multidimensional arrays using array_intersect

分類Dev

PHP: String to multidimensional array

分類Dev

Sort a multidimensional array in PHP

分類Dev

Php include with multidimensional array

分類Dev

How to build a multidimensional array tree from an associative array in PHP?

分類Dev

get a set of values from an array

分類Dev

Compare array values with same key in PHP

分類Dev

How to combine array to array multidimensional with object and key

分類Dev

Return specific values from array of hashes - JSON

分類Dev

Get values from array and using the values in a program

分類Dev

How to get all values of objects inside array

分類Dev

Write coordinates of array of values in multidimensional array

Related 関連記事

  1. 1

    PHP - search for key in multidimensional array and get its values deeper from the structure

  2. 2

    How to parse json to get all values of a specific key within an array?

  3. 3

    PHP Multidimensional Array Searching (Find key by specific value)

  4. 4

    PHP How to search multidimensional array for a key from another multidimensional array remembering parent key

  5. 5

    Built-in method to get flat array from a multidimensional array in PHP

  6. 6

    How to get a json object with specific key values, from a json array column?

  7. 7

    How to count all values in a multidimensional array?

  8. 8

    Remove specific keys from multidimensional array recursively

  9. 9

    Find Common most values in multidimensional array in PHP

  10. 10

    PHP multidimensional array sort matching values

  11. 11

    Parsing array inside object with unknown key and unknown values

  12. 12

    get key value from php array with string as key

  13. 13

    continue key for multidimensional array

  14. 14

    PHP - How to get the sum of a multidimensional array?

  15. 15

    Remove specific values from an array

  16. 16

    PHP function not returning a value from multidimensional array

  17. 17

    PHP extracting data from multidimensional array / JSON

  18. 18

    How to get common values from 4 multidimensional arrays using array_intersect

  19. 19

    PHP: String to multidimensional array

  20. 20

    Sort a multidimensional array in PHP

  21. 21

    Php include with multidimensional array

  22. 22

    How to build a multidimensional array tree from an associative array in PHP?

  23. 23

    get a set of values from an array

  24. 24

    Compare array values with same key in PHP

  25. 25

    How to combine array to array multidimensional with object and key

  26. 26

    Return specific values from array of hashes - JSON

  27. 27

    Get values from array and using the values in a program

  28. 28

    How to get all values of objects inside array

  29. 29

    Write coordinates of array of values in multidimensional array

ホットタグ

アーカイブ