Link to home
Start Free TrialLog in
Avatar of xenium
xenium

asked on

How to extract the values from a nested array into another array

I want to extract a column from a nested array, example:

Array
    (
        [0] => Array
            (
                [id] => 11
                [order_id] => 6127
            )

        [1] => Array
            (
                [id] => 12
                [order_id] => 6123
            )

    )

This is what I need:

$results = array()
$results(6127, 6123)

I'd like to do this concisely eg with an array_map or similar

Copied from here as I have a similar question and it was not answered here:
https://stackoverflow.com/questions/8873146/how-to-extract-the-values-from-a-nested-array-into-another-array
Avatar of xenium
xenium

ASKER

Sorry the question was answered:
$result = array_map(function($el) {    return $el['order_id'];
}, $arr);

Open in new window

Avatar of xenium

ASKER

Although answered, is it possible to do the same but referencing by column number instead of the name 'order_id' ?
Avatar of xenium

ASKER

The following is simpler, but only seems to work naming the column:

$arr_order_id = array_column($rows, 'order_id');

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial