Stdclass to Array

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);

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

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 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.

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));

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);

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

PHP - Remove an element from a stdclass array of stdclass objects

I'm afraid this is just one of those things you can't do much about.
It has to do with the way PHP was(not) designed to handle such cases. Basically you can not access an object property with a numeric variable name

This answer to a similar question will better explain:
https://stackoverflow.com/a/10333200/1012699

However, you can cast the object into an array and access/delete its content. Then cast back into an object if you so desire.
See the manual on type casting in php :
http://php.net/manual/en/language.types.type-juggling.php

Get data from an array of stdClass object

Considering you have:

    [urls] => stdClass Object
(
[url] => Array
(
[0] => stdClass Object
(
[type] => video
[_content] => http://vimeo.com/0000
)

)

)

Accessing an object is done via the object-access-operator (->), while accessing an array is done via square brackets ([x]).
So you end up with urls->url[0]->_content in this case. Since urls is an object, and url is an array, whose first ([0]) index contains another object.

So in short, to answer your full original question:
$object->urls->url[0]->_content is the URL
and
$object->thumbnails->thumbnail[0]->_content is the thumbnail



Related Topics



Leave a reply



Submit