You get the error because you are trying to turn a stdClass object into a string, something it doesn't support.
Instead of echo $decoded_traces try var_dump($decoded_traces) - that will give a diagnostic view of the object you've decoded (which I presume is what you wanted). You should find it looks like this
class stdClass#1 (2) {
public $Coords =>
array(5) {
[0] =>
class stdClass#2 (4) {
public $Accuracy =>
string(2) "65"
public $Latitude =>
string(18) "53.277720488429026"
public $Longitude =>
string(18) "-9.012038778269686"
public $Timestamp =>
string(39) "Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"
}
[1] => (more of the same, edited for brevity)
[2] =>
[3] =>
[4] =>
}
public $Scan =>
string(8) "Whatever"
}
By default, json_encode will create stdClass objects like the above. If you would prefer an associative array, pass true as the second parameter to json_decode, e.g. $decoded_traces=json_decode($traces, true);
As a further aside, although stdClass can't be turned into a string, your own classes can - a class which implements the __toString magic method could be converted to a string!
Answer from Paul Dixon on Stack OverflowYou get the error because you are trying to turn a stdClass object into a string, something it doesn't support.
Instead of echo $decoded_traces try var_dump($decoded_traces) - that will give a diagnostic view of the object you've decoded (which I presume is what you wanted). You should find it looks like this
class stdClass#1 (2) {
public $Coords =>
array(5) {
[0] =>
class stdClass#2 (4) {
public $Accuracy =>
string(2) "65"
public $Latitude =>
string(18) "53.277720488429026"
public $Longitude =>
string(18) "-9.012038778269686"
public $Timestamp =>
string(39) "Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"
}
[1] => (more of the same, edited for brevity)
[2] =>
[3] =>
[4] =>
}
public $Scan =>
string(8) "Whatever"
}
By default, json_encode will create stdClass objects like the above. If you would prefer an associative array, pass true as the second parameter to json_decode, e.g. $decoded_traces=json_decode($traces, true);
As a further aside, although stdClass can't be turned into a string, your own classes can - a class which implements the __toString magic method could be converted to a string!
use json_encode($traces) this will convert the array into the string. json_decode() is used to convert a string into array or object array
consuming graphiql error - Object of class stdClass could not be converted to string
PHP convert stdClass Object to string and reduce array with max value - Stack Overflow
php - foreach stdClass object converted to string - Stack Overflow
how to convert object into string in php - Stack Overflow
Hi all I have the following code https://pastebin.com/Emsgz88u
I've managed to get the data back from graphiql, however I cannot process the object. test2 at the bottom, I end up with a error Object of class stdClass could not be converted to string
I believe that I'm not handling the returned object correctly.
var_dump of one of the objects is
stdClass Object ( [name] => 20XX-LWxxxx:Law of xxxxxxxx ) object(stdClass)#3 (1) { ["name"]=> string(27) "20XX-LWxxxx:Law of xxxxxxxx" }
I've tried the following https://stackoverflow.com/questions/28098/php-equivalent-of-net-javas-tostring
and I've been following below to process the JSON coming back from graphiql https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php
Hope someone will help....Thanks... P
This will solve your purpose. let me know if anything goes wrong.
$ids[135][0]->ID = 145;
$ids[135][1]->ID = 135;
$ids[135][2]->ID = 155;
$ids[140][0]->ID = 125;
$ids[140][1]->ID = 135;
$idss = array();
foreach($ids as $k => $v) {
for($i = 0; $i < count($v); $i++) {
if(!@$idss[$k] || $v[$i]->ID > $idss[$k])
{
$idss[$k] = $v[$i]->ID;
}
}
}
echo "<Pre>";
print_r($idss);
die;
Already answered but here is a shorter version of this
$final_array =array();
foreach($arr as $key=>$val){
$max = max(array_keys($val));
$final_array[$key] = $val[$max]->ID ;
}
print_r($final_array);
Here $arr is your input array.
I can't comment, because I don't have that amount of reputation, but I'll try to answer...
I'm assuming you want to remove the stdClass from the PHP array?
If so, then make sure you are using true when decoding the JSON.
Example: $array = json_decode($json, true);
For more information on stdClass, see this SO post: What is stdClass in PHP?
Again, forgive me if I'm misinterpreting your question, I just wish I could comment...
I'm assuming this is from a response from the Australia Post postage calculation API. This API makes the horrible decision to treat collection instances with only one item as a single value. For example, service -> options -> option may be an array or it may be a single option.
The easiest way I've found to deal with this is cast the option to an array, eg
$options = $service->options->option;
if (!is_array($options)) {
$options = array($options);
}
// now you can loop over it safely
foreach ($options as $option) { ... }
You would do the same thing with the sub-options. If you're interested, I've got an entire library that deals with the AusPost API, using Guzzle to manage the HTTP side.
You can tailor how your object is represented as a string by implementing a __toString() method in your class, so that when your object is type cast as a string (explicit type cast $str = (string) $myObject;, or automatic echo $myObject) you can control what is included and the string format.
If you only want to display your object's data, the method above would work. If you want to store your object in a session or database, you need to serialize it, so PHP knows how to reconstruct your instance.
Some code to demonstrate the difference:
class MyObject {
protected $name = 'JJ';
public function __toString() {
return "My name is: {$this->name}\n";
}
}
$obj = new MyObject;
echo $obj;
echo serialize($obj);
Output:
My name is: JJ
O:8:"MyObject":1:{s:7:"*name";s:2:"JJ";}
Use the casting operator (string)$yourObject;
This works, creating the associate array and passing true to json_decode:
$string = '{"status": "success", "count": 3, "data": [{"id": 112233}]}';
$object = (object)json_decode($string, true);
var_dump($object);
object(stdClass)#1 (3) { ["status"]=> string(7) "success" ["count"]=> int(3) ["data"]=> array(1) { [0]=> array(1) { ["id"]=> int(112233) } } }
A much cleaner way would be:
$string = '{"status": "success", "count": 3, "data": [{"id": 112233}]}';
$object = json_decode($string);
check out what the output for print_r($object); looks like:
stdClass Object
(
[status] => success
[count] => 3
[data] => Array
(
[0] => stdClass Object
(
[id] => 112233
)
)