Convert a PHP object to an associative array

0 votes
I'm integrating an API to my website which works with data stored in objects. My code is written using arrays and I would like a quick-and-dirty function to convert an object to an array. Any help will be appreciated!!
Feb 23, 2022 in PHP by Rahul
• 9,680 points
2,085 views

1 answer to this question.

0 votes

Start with simply typecasting the line:-

$array = (array) $yourObject;

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions such as the integer properties are inaccessible and protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.


Example: Simple Object

$object = new StdClass; 
$object->foo = 1; 
$object->bar = 2; 

var_dump( (array) $object );

Output:

array(2) { 
        'foo' => int(1) 
        'bar' => int(2) 
}

Example: Complex Object

class Foo 
{ 

        private $foo; 
        protected $bar; 
        public $baz; 

        public function __construct() 
        { 

                  $this->foo = 1; 
                  $this->bar = 2; 
                  $this->baz = new StdClass; 
        } 
} 

var_dump( (array) new Foo );

Output (with \0s edited in for clarity):

array(3) { 
      '\0Foo\0foo' => int(1) 
      '\0*\0bar' => int(2) 
      'baz' => class stdClass#2 (0) {} 
}

Output with var_export instead of var_dump:

array ( 
      '' . "\0" . 'Foo' . "\0" . 'foo' => 1, 
      '' . "\0" . '*' . "\0" . 'bar' => 2, 
      'baz' => 
      stdClass::__set_state(array( 
  )), 
)

Typecasting this way will not do deep casting of the object graph and you need to apply the null bytes to access any non-public attributes. So this works best when casting StdClass objects or objects with only public properties.

answered Feb 23, 2022 by Aditya
• 7,680 points

Related Questions In PHP

0 votes
0 answers

How to convert an array to a string in PHP?

What can I do to get the ...READ MORE

May 30, 2022 in PHP by Kichu
• 19,040 points
622 views
0 votes
0 answers

How to convert a string to an array in php

I want to convert a string into ...READ MORE

Jun 1, 2022 in PHP by Kichu
• 19,040 points
539 views
0 votes
0 answers

How to convert an array to object in PHP?

How can I convert an array like ...READ MORE

Jun 18, 2022 in PHP by narikkadan
• 63,600 points
491 views
0 votes
0 answers

How to sort an array of associative arrays by value of a given key in PHP?

Given this array: $inventory = array( ...READ MORE

Jul 24, 2022 in PHP by Kithuzzz
• 38,000 points
651 views
0 votes
1 answer

How to check if array is multidimensional or not?

Since the 'second dimension' could be just ...READ MORE

answered Nov 5, 2018 in Others by DataKing99
• 8,250 points
5,955 views
0 votes
1 answer

How to store input value into array then localstorage?

Hello @ abhittac, You have create the array everytime the ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,840 points
9,176 views
0 votes
0 answers

php var_dump() vs print_r()

What is the difference between var_dump() and print_r() in terms of ...READ MORE

May 28, 2022 in PHP by Kichu
• 19,040 points
520 views
0 votes
0 answers

How can I sort arrays and data in PHP?

How do I sort an array in ...READ MORE

May 29, 2022 in PHP by Kichu
• 19,040 points
420 views
0 votes
1 answer

How do I convert a string to a number in PHP?

You don't have to do this, since ...READ MORE

answered Feb 23, 2022 in PHP by Aditya
• 7,680 points
594 views
0 votes
1 answer

How do I strip all spaces out of a string in PHP?

If I have understood your question right, ...READ MORE

answered Feb 23, 2022 in PHP by Aditya
• 7,680 points
1,200 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP