I have this data
$result = json_decode(curl_exec($ch));
print_r($result);
// stdClass Object ( [d] => {"id":10} )
$id = ?;
How can I get a value of id?
Since $result is an object, you have to use property notation to access its components.
$id = json_decode($result->d)->id;
You need the extra json_decode because the value of $result->d is another JSON string, not an object or array.
You would get value with following
$obj_result = json_decode($result->d);
echo $obj_result->id;
By this way you will get id value 10
Related
I'm working with Laravel 5 right now and I have the following problem. I've got response from DB query:
[{"id":1}]
and I want to take out 1 as int or string. Any ideas?
I've tried to solve this like follows:
$json = (DB query);
$data = json_decode($json);
$final = $data[0]->id;
and response is :
json_decode() expects parameter 1 to be string, array given
This is all you need.
$response = json_decode($response); // Decode the JSON
$string = $response[0]->id; // Save the value of id var
As you say, the string you have is in JSON format, so you need to use json_decode to access it in PHP.
The square brackets refer to an array, and the braces refer to an object within that array, so what you're looking for is the id value of the first element (i.e. element 0) in the array.
<?php
$json = '[{"id":1}]';
$data = json_decode($json);
echo $data[0]->id;
// 1
Try this
$json_array = '[{"id":1}]';
$data = json_decode($json_array);
print_r($data); // To display the result array
You just need to decode it, if I'm not misunderstanding your questions.
$json = '[{"id":1}]';
$decodedObject = json_decode($json);
Then you can loop through the aray and do something with your data:
foreach($decodedObject as $key => $value) {
$id = $value->id;
}
If you're using Laravel, though, why not use Eloquent models?
i am laraval5.1. i am taking database table into array.
data is getting stored into stdclassobject.
i want to convert into normal array. how to do that
$silver_plans = array();
$silver_plans = DB::select('select * from ins_gold ');
print_r($silver_plans);
Dont know how to convert stdclass object into normal array
after storing database table ... by default it is storing data into into stdclass object. but i want to store into array
You can try this (built in in laravel 5):
$silver_plans = DB::select('select * from ins_gold ')->toArray();
print_r($silver_plans);
$array = (array) $yourObject;
check Convert PHP object to associative array
I have a result, $result, returned from an SQL query which contains the following data: [{"TOTAL":"12345"}]
I want to put that result into a JSON API with a route of, say, /api/total/ and have it return: {total:12345}. I'll be setting the first half of that manually, e.g. 'total' => whatever in the Slim framework.
$app->render(200, array(
'total' => $result["TOTAL"]
)
);
How do I reference 12345 from the returned array? Doing $result["TOTAL"] doesn't work.
The result looks like [{"TOTAL":"12345"}]?
So you have to json_decode it first.
$decodedResult = json_decode($result, true);
echo $decodedResult[0]['TOTAL'];
Use this code , you will get the value.
$result = '[{"TOTAL":"12345"}]';
$res = json_decode($result);
echo $res[0]->TOTAL;
Please try this
$res = json_decode($result);
even though there is only one object in the array, it is still an array so you need to reference the object's index.
$result[0]["TOTAL"]
I'm requesting data from an online source which I then decode into json StdClass objects (using php). Once I've done this I have the following (see below). I'm trying to extract the elements in 'otherstuff' by doing echo $response->stuff->WHAT GOES HERE?->otherstuff
However I cant hard code the [2010-12] because its a date, is there any way I can call e.g. $response->stuff->nextsibling->stuff
I hope this makes sense to someone :D Currently i'm bastardising this with a $key => $value for loop and extracting the key value and using it in my $response->stuff->$key->stuff call.
stdClass Object
(
[commentary] =>
[stuff] => stdClass Object
(
**[2010-12]** => stdClass Object
(
[otherstuff] => stdClass Object
(
[otherstuffrate] => 1
[otherstufflevel] => 1
[otherstufftotal] => 1
)
)
)
)
StdClass instances can be used with some Array Functions, among them
current — Return the current element in an array and
key — Fetch a key from an array
So you can do (codepad)
$obj = new StdClass;
$obj->{"2012-10"} = 'foo';
echo current($obj); // foo
echo key($obj); // 2012-10
On a sidenote, object properties should not start with a number and they may not contain dashes, so instead of working with StdClass objects, pass in TRUE as the second argument to json_decode. Returned objects will be converted into associative arrays then.
The date key must be a string, otherwise PHP breaks ;).
echo $response->stuff['2010-12']->otherstuff
Retrieve it using a string.
Edited again: added object code also
json decode it as associative array, and use key fetched through array_keys . See it work here : http://codepad.org/X8HCubIO
<?php
$str = '{
"commentary" : null,
"stuff" : {
"ANYDATE" : {
"otherstuff": {
"otherstuffrate" : 1,
"otherstufflevel" : 1,
"otherstufftotal" : 1
}
}
}
}';
$obj = json_decode($str,true);
$reqKey = array_keys($obj["stuff"]);
$req = $obj["stuff"][$reqKey[0]]["otherstuff"];
print_r($req);
print "====================as object ============\n";
$obj = json_decode($str);
$req = current($obj->stuff)->otherstuff;
print_r($req);
?>
I'm having trouble getting the value of the last insert Id from doctrine. I did a bit of research and I found that the following should work:
//save the store now
$store = new Store();
$store->url = $this->form_validation->set_value('website');
$store->save();
$store_id = $store->identifier();
echo print_r($store->identifier());
Returns
Array ( [id] => 1000034 )
How do I pull just the value (1000034) out of the array and set it in a variable that I can use elsewhere?
Your $store_id is an associative array with a single value keyed by "id" so:
$id = $store_id['id'];