K
K
Kerm2021-06-09 14:36:27
PHP
Kerm, 2021-06-09 14:36:27

How to get an array with values ​​found by key from a multidimensional array?

Array example:

$array = [
            "PaymentInfo" => [
                "paymentInfo" => "олролро",
                "fileList" => [
                    "fileCode" => "3623"
                ],
            ],
            "PaymentInfo2" => [
                "paymentInfo2" => "олролро",
                "fileList" => [
                    "fileCode" => "3623"
                ],
                "files" => [
                        "name" => "dfgdfgd",
                        "file" => [
                                'fileCode' => "6486"
                            ]
                    ]
            ],
            "file" => 
                [
                    "fileCode" => "3623"
                ],[
                    "fileCode" => "3623"
                ]
        ];


The array can have many fileCode keys at different levels of the array, you need to find everything and create an array from them, something like [0 => "3623", 1 => "3650", 2 => "3670"]

I would like to implement with using the usual php methods working with arrays, without manually writing recursive methods. I tried array_map, array_column, although they write there that they work with multidimensional arrays, as a result, with a similar array, they do not work as they should.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-06-09
@Kerm

$fileCodes = [];
array_walk_recursive($array, function($v, $k) use(&$fileCodes) {
  if ($k === 'fileCode') {
    $fileCodes[] = $v;
  }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question