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
WhenTRUE
, 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);
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;
How to convert an array into an object using stdClass()
You just add this code
$clasa = (object) array(
'e1' => array('nume' => 'Nitu', 'prenume' => 'Andrei', 'sex' => 'm', 'varsta' => 23),
'e2' => array('nume' => 'Nae', 'prenume' => 'Ionel', 'sex' => 'm', 'varsta' => 27),
'e3' => array('nume' => 'Noman', 'prenume' => 'Alice', 'sex' => 'f', 'varsta' => 22),
'e4' => array('nume' => 'Geangos', 'prenume' => 'Bogdan', 'sex' => 'm', 'varsta' => 23),
'e5' => array('nume' => 'Vasile', 'prenume' => 'Mihai', 'sex' => 'm', 'varsta' => 25)
);
If you want to see is this stdClass object just call this
print_r($clasa);
If you want to convert an array to object code will be
$arr = array('a'=>'apple','b'=>'ball');
$arr = (object) $arr;
You don't need to use stdClass. It will automatically converted to stdClass
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.
stdClass Object In Array Iteration
This is easily enough done with array_column()
to get the ID values; it works just as well with objects as it does with arrays.
<?php
// some sample data
$results = json_decode('[{"ID": 1}, {"ID": 2}, {"ID": 3}]');
$return = implode(",", array_column($results, "ID"));
echo $return;
Output:
1,2,3
Get value of array stdClass Object
Each element in the "charges" array is an object. A different syntax is used to refer to the members of the object:
//for an array
echo $someArray['someValue'];
//for an object
echo $someObject->someValue;
echo $someObject->getSomeValue()
Therefore, this is the way for you:
echo $return->data->charges[0]->code;
Or, step by step:
var_dump($return);
var_dump($return->data);
var_dump($return->data->charges);
var_dump($return->data->charges[0]);
var_dump($return->data->charges[0]->code);
var_dump($return->data->charges[0]->code->billetDetails);
var_dump($return->data->charges[0]->code->billetDetails->bankAccount);
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));
Related Topics
PHP: How to Remove Specific Element from an Array
PHP Get All Subdirectories of a Given Directory
How to Detect Duplicate Values in PHP Array
What Type of Hash Does Wordpress Use
How to Properly Use a Pdo Object For a Parameterized Select Query
Converting HTML to Plain Text in PHP For E-Mail
Passing JavaScript Array to PHP Through Jquery $.Ajax
How to Return Integer and Numeric Columns from MySQL as Integers and Numerics in PHP
Save Image from Url With Curl PHP
Continue PHP Execution After Sending Http Response
Is Micro-Optimization Worth the Time
"Smtp Error: Could Not Authenticate" in PHPmailer
Mixing a PHP Variable With a String Literal
How to Create a Copy of an Object in PHP
How to Resolve Curl Error (7): Couldn't Connect to Host