$postdata= "stdClass Object
(
[created] => 1326853478
[livemode] =>
[id] => evt_00000000000000
[type] => invoice.payment_succeeded
...........................
))";
$event = json_decode($postdata);
echo "<pre>";
print_r($event);
Can't std class object to array in php?I am using json_decode but its not working just get empty array.I am giving my result data std class object assign in variable its anything wrong that's why its showing not working?
you can use :
$array = json_decode(json_encode($data), true);
print_r($array);
The easiest way is to cast it:
$array = (array) $stdClass;
try this
$event = json_decode($data, true);
print_r($event);
Related
I have an array like below. I want to extract the values . Help me out please. But this doesn't print anything. Please help me.Any help would be appreciated.May you all find this question similar.But I am unable to find any answer,because that's the way we do to find the array values.
Array
(
[0] => stdClass Object
(
[bHeader] => stdClass Object
(
[ei] => NSE
[seg] => I
)
[cNetChangeIndicator] =>
[fClosingIndex] => 10558.5
[fHighIndexValue] => 10532
[fIndexValue] => 10469
[fLowIndexValue] => 10438.5
[fOpeningIndex] => 10499.5
[fPercentChange] => -0.85
[sIndexName] => 962450
[fChange] => -89.5
[iIdxId] => 311
)
)
Thanks in advance
convert your object in to array using
$array = (array) $yourObject;
if you use json_decode than give second parameter true e.g
$array = json_decode($jsonStr,TRUE);
It will return array so no need to typecast(conveting) obj to array
also used operator '->' which help to fetch data from object
You are accessing the object in the array as if it is also an array.
You need to access the object's properties using ->
echo $arr[0]->fIndexValue;
echo $arr[0]->fChange;
echo $arr[0]->fPercentChange';
For example:
$obj = new stdClass;
$obj->fIndexValue = 10469;
$arr = array();
$arr[0] = $obj;
echo $arr[0]->fIndexValue;
Prints "10469".
Try this to print the whole thing, assuming your var is $arr:
print_r($arr);
Or for variables
print($arr[0]-->fIndexValue);
i am new to JSON i have a json object retrieved from the database in the form of
Array
(
[0] => stdClass Object
(
[id] => 1
[data] => {"vehicle":[{"year":"2000","make":"Ac","model":"Aceca","acquired_year":"2016","acquired_month":"2","use":"Business","distance_driven_to_work_or_school":"2","distance_driven_manually":"10000"}],"first_name":"ADAS","last_name":"DSADSADA","email":"asddsa#sda.com","phone":"dsasasa","postal_code":"","drivers":[{"name":"ssada","birth_year":"2016","birth_month":"2","birth_day":"2","gender":"female","martial_status":"Single","license_number_provided":"yes","license_number":"asddasdas","license_type":"","training_completed":"","years_been_listed_on_auto_policy_in_north_america":"No Previous Experience","license_suspensions":"","accidents":"Select","convictions":"Select","cancellation_reason":"","cancellation_year":"","cancellation_month":"","cancellation_day":""}],"considering_renters_to_reduce_rate":"yes","install_winter_tires":"no","park_in_private_driveway":"yes","willing_to_install_device":"no","years_insured_with_current_company":"4 Years","how_heard_about_us":"asdaa"}
[date] => 2017-11-20 18:17:52
[status] => 0
)
)
now when i try to use json_decode to convert it into an array i am getting Trying to get property of non-object here's my code
<?php
echo "<pre>";
print_r($quotes); //works fine uptil here
$data = json_decode($quotes->data,true);//the error line
echo "<pre>";
print_r($data);
?>
i tried it a couple of ways but it is not working i tried some other solutions as well stil ending up getting errors any help?
It is because $quotes is an array of objects. Try $quotes[0]->data, e.g.:
$data = json_decode($quotes[0]->data,true);
// ------------------------^^^
You're receiving an array containing objects from the database. You're almost there but instead of
$data = json_decode($quotes->data,true);
You should use
$data = json_decode($quotes[0]->data,true);
I'm playing with an API that's giving me data back in JSON format that I then json_decode() to the following format:
[stockData] => stdClass Object
(
[data] => stdClass Object
(
[PS3] => stdClass Object
(
[2015-01-26T20:45:01Z] => stdClass Object
(
[AMU] => 999.76
[ZIT] => 3.63
)
)
)
[status] => stdClass Object
(
[code] => 200
[text] => ok
)
)
I need some way of getting the 2015-01-26T20:45:01Z (which changes all the time).
I've tried get_Class() on the object, eg:
get_Class($bawsaq->stockData->data->PS3) (actually in a foreach loop)
But all that's returned is: "stdClass" and not the name. How can I get the object's name?
It isn't actually the object's class: it's the name of the property that contains the stdClass object. So you'd need to get the first object property name from $bawsaq->stockData->data->PS3. Which is a bit tricky, actually.
It's nicer to work with arrays. If you use the $assoc parameter of json_decode, you can get an associative array instead of an object whenever a JSON object appears. This is much easier to deal with in PHP.
$bawsaq = json_decode($jsonData, true);
You can get the key name with key:
$dateTime = key($bawsaq['stockData']['data']['PS3']);
When you decode the JSON, use
$bawsaq = json_decode($json, true);
This will return associative arrays instead of stdClass objects for all the JSON objects. Then you can use
$keys = array_keys($bawsaq['stockData']['data'];
$date = $keys[0];
You can use get_object_vars method.
$obj = new stdClass();
$obj->field1 = 'value1';
print_r(get_object_vars($obj));
Result:
Array
(
[field1] => value1
)
You can use the second argument to json_decode. This will return the data as an associative array instead of an object list, so you could simply use
$input = json_decode($jsonInput, true);
$key = key($input['stockData']['data']['PS3']);
$data = $input['stockData']['data']['PS3'][$key];
or a foreach-loop. See also key on php.net.
i have:
stdClass Object
(
[0] => stdClass Object
(
[one] => aaa
[two] => sss
)
[1] => stdClass Object
(
[one] => ddd
[two] => fff
)
[2] => stdClass Object
(
[one] => ggg
[two] => hhh
)
}
and i must get this with keys, for example:
$var = $stdClass[0];
but i have error:
Fatal error: Cannot use object of type stdClass as array in
Is possible parse this stdClass to array and use this with keys?
Cast it to an array:
$array = (array)$stdClass;
If you're using json_decode to convert that JSON string into an object, you can use the second parameter json_decode($string, true) and that will convert the object to an associative array.
If not, what everybody else has said and just type cast it
$array = (array) $stdClass;
Your problem is probably solved since asking, but for reference, quick uncle-google answer:
function objectToArray($d) {
if(is_object($d)) {
$d = get_object_vars($d);
}
if(is_array($d)) {
return array_map(__FUNCTION__, $d); // recursive
} else {
return $d;
}
}
Full article here. Note I'm not associated with the original author in any way.
Cast it
$array = (array) $stdObject;
Of course you can typecast, $var = (array) $obj;, but I would suggest ArrayAccess to your class.
By using ArrayAccess, you can then treat your objects and data as if it was an array, or natively as an object.
Cast it into an array. Currently it is not readable to PHP as an array.
$array = (array)$stdClass;
Essentially, just type cast it:
$arr = (array)$obj;
$var = $arr[0];
But read the caveats here.
If you have an nested array you can used json_encode and json_decode to convert the whole object to an array:
$result = json_decode(json_encode($source), JSON_OBJECT_AS_ARRAY);
This one worked for me,
The decoding and encoding makes for a regular array
$array = json_decode(json_encode($object), True);
function load_something () : \stdClass {
$result = new \stdClass();
$result->varA = 'this is the value of varA';
$result->varB = 'this is the value of varB';
$result->varC = 'this is the value of varC';
return $result;
}
$result = load_something();
echo ($result instanceof stdClass)?'Object is stdClass':'Object is not stdClass';
echo PHP_EOL;
print_r($result);
//directly extract a variable from stdClass
echo PHP_EOL . 'varA = ' . ($result->varA);
//convert to array, then extract
$array = (array)$result;
echo PHP_EOL . 'varA = ' . $array['varA'];
stdClass is an object so u can access value from it like
echo stdClass->one;
This question already has answers here:
Convert a PHP object to an associative array
(33 answers)
Closed 1 year ago.
My array is like:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => demo1
)
[1] => stdClass Object
(
[id] => 2
[name] => demo2
)
[2] => stdClass Object
(
[id] => 6
[name] => otherdemo
)
)
How can I convert the whole array (including objects) to a pure multi-dimensional array?
Have you tried typecasting?
$array = (array) $object;
There is another trick actually
$json = json_encode($object);
$array = json_decode($json, true);
You can have more info here json_decode in the PHP manual, the second parameter is called assoc:
assoc
When TRUE, returned objects will be converted into associative arrays.
Which is exactly what you're looking for.
You may want to try this, too : Convert Object To Array With PHP (phpro.org)
Just use this :
json_decode(json_encode($yourArray), true);
You can use array_walk to convert every item from object to array:
function convert(&$item , $key)
{
$item = (array) $item ;
}
array_walk($array, 'convert');
Assuming you want to get to this pure array format:
Array
(
[1] => "demo1",
[2] => "demo2",
[6] => "otherdemo",
)
Then I would do:
$result = array();
foreach ($array as $object)
{
$result[$object->id] = $object->name
}
(edit) Actually that's what I was looking for possibly not what the OP was looking for. May be useful to other searchers.
You should cast all objets, something like :
$result = array();
foreach ($array as $object)
{
$result[] = (array) $object
}
As you are using OOP, the simplest method would be to pull the code to convert itself into an array to the class itself, you then simply call this method and have the returned array populate your original array.
class MyObject {
private $myVar;
private $myInt;
public function getVarsAsArray() {
// Return the objects variables in any structure you need
return array($this->myVar,$this->myInt);
}
public function getAnonVars() {
// If you don't know the variables
return get_object_vars($this);
}
}
See: http://www.php.net/manual/en/function.get-object-vars.php for info on get_object_vars()
it you have object and you want to set a value as array
use
$this->object->pluck('name');
then you get the value as array of names like
["name1", "name2", "name3"];