Convert Stdclass Object to Array in PHP

Convert stdClass object to array in PHP

The easiest way is to JSON-encode your object and then decode it back to an array:

$array = json_decode(json_encode($object), true);

Or if you prefer, you can traverse the object manually, too:

foreach ($object as $value) 
$array[] = $value->post_id;

php stdClass to array

The lazy one-liner method

You can do this in a one liner using the JSON methods if you're willing to lose a tiny bit of performance (though some have reported it being faster than iterating through the objects recursively - most likely because PHP is slow at calling functions). "But I already did this" you say. Not exactly - you used json_decode on the array, but you need to encode it with json_encode first.

Requirements

The json_encode and json_decode methods. These are automatically bundled in PHP 5.2.0 and up. If you use any older version there's also a PECL library (that said, in that case you should really update your PHP installation. Support for 5.1 stopped in 2006.)


Converting an array/stdClass -> stdClass

$stdClass = json_decode(json_encode($booking));

Converting an array/stdClass -> array

The manual specifies the second argument of json_decode as:

assoc

When TRUE, returned objects will be converted into associative arrays.

Hence the following line will convert your entire object into an array:

$array = json_decode(json_encode($booking), true);

Laravel - How to convert Stdclass object to Array

Please try like this

$result = json_decode(json_encode($data, true));

convert std object array to simple php array

You can use the (array) type cast to convert an object to an equivalent associative array.

$data = array_map(function($x) { return (array)$x; }, $data);

If the original array of objects came from using json_decode(), you can tell it to return associative arrays instead of objects by giving it a true second argument.

$data = json_decode($data, true);

php, how to convert an array containing stdClass Object into comma separated list

Except for the comma separated list, what you have should work in theory, but the code doesn't at all match your array. You state $object not $array and the property is id not nid.

However, just extract the id column and implode. If you have PHP 7:

$list = implode(', ', array_column($output, 'id'));

For older versions:

$list = implode(', ', array_map(function($v) { return $v->id; }, $output));

Std class object to array

What does your model look like where this data is being generated? If you query your database to retrieve arrays you won't have to worry about converting the data.

In codeigniter you can use row_array() instead of row() or result_array() instead of result()

For example change this:

$query = $this->db->get('mytable');
$query->get->row();
return $query;

Into this:

$query = $this->db->get('mytable');
$query->get->row_array();
return $query;

In this example I'm using row()/row_array(), but the same applies for result()/result_array()

How to parse stdClass object in php

You can use json_encode to convert it to a json. After which you can use json_decode with the second parameter set to true which will return an array instead of an StdObject.

If the api you're calling simply returns a json itself then you can go ahead and use json_decode directly with the second argument set to true to yield the same result.

There are other ways, but this is the simplest.

You can check out other questions, like this one or this one.



Related Topics



Leave a reply



Submit