3

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

3
  • 2
    Use array_values() after that. Commented Aug 3, 2017 at 15:57
  • 2
    With private properties like that, get_object_vars should have returned an empty array, not an associative array. Commented Aug 3, 2017 at 15:58
  • Why you cannot use associative array? It's mostly the same to use array(0 => "1", 1 => "2"); or array('a' => 1, 'b' => 2); Commented Aug 3, 2017 at 16:00

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thx but it is not take smth like : ["date":"responseObject":private]=> string(10) "03-08-2017" i
That looks like a reflection result. See the results at 3v4l.org as added in my answer for evidence that get_object_vars alone does not provide private values.

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.