0

This is the array I want to extract from:

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)

There can be any number of arrays within the first array.

2 Answers 2

1

Make use of array_map:

$result = array_map(function($el) {
    return $el['order_id'];
}, $arr);

Or for PHP version prior to 5.3:

$result = array_map(create_function('$el', 'return $el["order_id"];'), $arr);
Sign up to request clarification or add additional context in comments.

Comments

0

Iterate through the array then add the order ids to another array:

$results = array();
foreach($arr as $item)
{
    $results[] = $item['order_id'];
}

2 Comments

@Patrick: It's that 4th line of code: $results[] = $item['order_id']; -- $item['order_id'] is added as a new item to the array $results.
gotcha. thank you. I'm still learning. I struggle with arrays. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.