This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 8 months ago.
{"status":"ok","params":{"stream_token":"token=lTQ4sJx9vK7pR7kgeYVDgQ&e=1448284525&u=37997"}}
I would like to parse this string and need
token=lTQ4sJx9vK7pR7kgeYVDgQ&e=1448284525&u=37997
Hows that possible in PHP?
echo json_decode('{"status":"ok","params":{"stream_token":"token=U8h5Ma12SrlizPoFm-Nc5w&e=1448285819&u=37997"}}');
throuse the following error:
Catchable fatal error: Object of class stdClass could not be converted
to string
Add the extra parameter to json_decode to retrieve the result as an associative array:
$data = json_decode('{"status":"ok","params":{"stream_token":"token=U8h5Ma12SrlizPoFm-Nc5w&e=1448285819&u=37997"}}', TRUE);
$url= $data['params']['stream_token'];
var_dump($url); // token=U8h5Ma12SrlizPoFm-Nc5w&e=1448285819&u=37997"
To parse the url variables use parse_str:
parse_str($url, $fragments);
var_dump($fragments);//
/*
array(3) {
["token"]=>
string(22) "U8h5Ma12SrlizPoFm-Nc5w"
["e"]=>
string(10) "1448285819"
["u"]=>
string(5) "37997"
}
*/
Related
This question already has answers here:
How to solve PHP error 'Notice: Array to string conversion in...'
(6 answers)
Closed 5 years ago.
I can't access my json and would like to know where the error is.
$json = json_encode($data);
// string(1065) "[{"id":"test123","key":["one",...
Then I decode it to make it accessable via this command:
$json = json_decode($json, true);
// Output:
array(3) {
[0]=>
array(4) {
["id"]=>
string(14) "test123"
When I want to echo it, it gives me:
echo $json[0];
Array to string conversion in
Use print_r for array, echo is used to display a string that's why it's giving error.
print_r($json);
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 5 years ago.
I'm writing a PHP plugin and need to extract a string from an object which is inside an array.
var_dump($data)
outputs:
array(1) {
[0]=> object(stdClass)#380 (53) {
["id"]=> string(1) "2"
["firstname"]=> string(6) "John"
["lastname"]=> string(6) "Doe"
["email"]=> string(31) "johndoe#email.com"
}
}
I want to return:
johndoe#mail.com
My research has turned up functions such as unserialize, array_map, array_slice, and array_column, but I haven't found the right function or combination of functions that work together (nor do I understand them enough) to just return a simple string from an object inside an array. Can anyone provide me with some guidance?
Edit: This isn't the same question as the one referenced as "the exact same question." The other one asks simply how to access an array (answer: $array[0]), and my question asked how to access an object INSIDE an array (answer: $array[0]->text).
For example:
$data[0]->firstname
Maybe:
$arr = get_object_vars($data[0]);
$arr['firstname'];
Example:
$obj = (object) array('x' => 'foo');
$arr = [$obj];
print($arr[0]->x);
You will get: foo
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
I have this JSON response:
object(stdClass)#1 (2) {
["data"]=>
array(47) {
[0]=>
object(stdClass)#2 (4) {
["id"]=>
int(341)
["competition_id"]=>
int(1)
["name"]=>
string(9) "2015/2016"
["active"]=>
bool(true)
But I don't know how to parse the data.
I tried it with this PHP code:
echo $json->data[0]->id;
But it doesn't work. How can I get the ID for example?
here is my solution:
$json = file_get_contents($url);
$json2 = json_decode($json);
echo $json2->data[0]->id;
sorry for complication, the goal was the json_decode() function, after that I can get the data with "->"
greetings!
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 7 years ago.
Just a real quick question please, I have this string that came from my query
I am able to display the string using xx = dd($sumx) which gave me the string below:
string(124) "[{"total":-4107717.58,"alerx":4,"currentYear":-4107717.58,"lastYear":0,"date":2015,"value":{"debit":0,"credit":4107717.58}}]"
in order for me to access it via '->' notation, like an object. I convert it using json_decode()
I echoed it again and gives me this object format:
array(1) {
[0]=>
object(stdClass)#508 (6) {
["total"]=>
float(-4107717.58)
["alerx"]=>
int(4)
["currentYear"]=>
float(-4107717.58)
["lastYear"]=>
int(0)
["date"]=>
int(2015)
["value"]=>
object(stdClass)#509 (2) {
["debit"]=>
int(0)
["credit"]=>
float(4107717.58)
}
}
}
when I tried to access $sumx->value it then gives me a `Trying to get property of non-object
I tried accessing it via $sumx[0]->value same error, I also tried validating the string to http://jsonlint.com/ and it says its valid. Can someone point out to me whats wrong please. Thanks for the time and have a good day.
Try like below:-
<?php
$data = '[{"total":-4107717.58,"alerx":4,"currentYear":-4107717.58,"lastYear":0,"date":2015,"value":{"debit":0,"credit":4107717.58}}]';
$new_array = json_decode($data);
echo "<pre/>";print_r($new_array);
echo "<pre/>";print_r($new_array['0']->value);
echo "<pre/>";print_r($new_array['0']->value->debit);
echo "<pre/>";print_r($new_array['0']->value->credit);
?>
Output:- https://eval.in/381395
Note:- change is $new_array['0']->value only.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Special characters in property name of object
I'm parsing a JSON response from MediaWiki's API: Requested URL
I run it through json_decode() and then pull some pieces of it. The problem I have is that the attribute name for the content is *. I get a PHP error when I try to access the content ( the 140,950 character string at the end of my vardumped json_decoded example below ) like this:
foreach( $Response->query->pages as $page_id => $Page ) {
$this->id = $Page->pageid;
$this->title = $Page->title;
$this->content_with_markup = $Page->revisions[0]->*;
}
The PHP Error: PHP Parse error: syntax error, unexpected '*'
The pageid and title work fine.
Here is the piece of JSON_Decoded object that is giving me problems:
object(stdClass)#5 (1) {
["11968"]=>
object(stdClass)#6 (4) {
["pageid"]=>
int(11968)
["ns"]=>
int(0)
["title"]=>
string(17) "George Washington"
["revisions"]=>
array(1) {
[0]=>
object(stdClass)#7 (2) {
["contentformat"]=>
string(11) "text/x-wiki"
["*"]=>
string(140950) "{{About|the first President of the United States|other uses}}...
How do I access the content contained in the attribute named *?
Same as always.
...->{'*'}->...