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 in PHP by Rahul
• 8,980 points
39 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 by Aditya
• 7,280 points

Related Questions In PHP

0 votes
1 answer

How to convert a string to JSON object in PHP?

Hello @kartik, it seems that your JSON is ...READ MORE

answered Oct 20, 2020 in PHP by Niroj
• 82,680 points
1,929 views
0 votes
1 answer

How to pass an array within a query string?

Hello, Submitting multi-value form fields, i.e. submitting arrays ...READ MORE

answered Apr 8, 2020 in PHP by Niroj
• 82,680 points
9,346 views
0 votes
1 answer

How do I convert a PDF document to a preview image in PHP?

Hello @kartik, You need ImageMagick and GhostScript <?php $im = new imagick('file.pdf[0]'); $im->setImageFormat('jpg'); header('Content-Type: image/jpeg'); echo ...READ MORE

answered Aug 14, 2020 in PHP by Niroj
• 82,680 points
2,658 views
0 votes
1 answer

How to remove duplicate values from an array in PHP?

Hello @kartik, Use array_unique(): Example: $array = array(1, 2, 2, 3); $array ...READ MORE

answered Sep 15, 2020 in PHP by Niroj
• 82,680 points
1,283 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,240 points
3,863 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,680 points
4,203 views
+1 vote
2 answers

Scp Php files into server using gradle

Tru something like this: plugins { id ...READ MORE

answered Oct 11, 2018 in DevOps & Agile by lina
• 8,220 points
507 views
0 votes
1 answer

How do I create folder under an Amazon S3 bucket through PHP API?

Of Course, it is possible to create ...READ MORE

answered Apr 24, 2018 in AWS by anonymous
8,991 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 in PHP by Aditya
• 7,280 points
44 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 in PHP by Aditya
• 7,280 points
82 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP