Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get object values as array in php

Tags:

object

php

I have object with setter and getter

  class obj{
     private $a;
     private $b;

  }

I would like to create an array that get only the value of the object , for example

array = ["1","2"]

tried get_object_vars but its an associative array

like image 474
fstdv Avatar asked Aug 18 '26 00:08

fstdv


1 Answers

To condense an associative array to an indexed array, use array_values:

array_values(get_object_vars($object));

However, given that you have private member variables, you might just want to use an array cast instead of get_object_vars:

array_values((array)$object);

See it online at 3v4l.org.

like image 127
bishop Avatar answered Aug 20 '26 15:08

bishop