0

How can I convert and object like this:

Array(
   Object(
       'id' => 1
   ),
   Object(
       'id' => 2
   ),
   Object(
       'id' => 3
   ),
)

To and array like this:

Array(1,2,3)

Without using a for loop. Is there some pre-built function in php to accomplish that?

1

1 Answer 1

2

The built in function in PHP is array_column

$ids = array_column($array, 'id');

It will fetch all ids from the inner objects/associative arrays and put them in a unidimensional array.

$ids = [1, 2, 3];
Sign up to request clarification or add additional context in comments.

Comments

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.