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!
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I have the following code:
$response = $client->get_json($query);
var_dump($response);
which shows me this in a browser:
object(stdClass)#8 (7) {
["search_metadata"]=>
object(stdClass)#7 (8) {
["id"]=>
string(24) "5fa06b463ffd1f75b14c1b98"
["status"]=>
...
}
["search_parameters"]=>
object(stdClass)#9 (9) {
["engine"]=>
string(6) "google"
["q"]=>
...
}
["inline_images"]=>
array(10) {
[0]=>
object(stdClass)#11 (2) {
["link"]=>
string(231) "/search?q=796714619071&num=30&gl=us&hl=en&tbm=isch&source=iu&ictx=1&fir=iYsIHfZTBqNwZM%252Cfl2S346slZj2tM%252C_&vet=1&usg=AI4_-kRkH4vMP2OKxQ8Mz6SJlNoImL7gPg&sa=X&ved=2ahUKEwiCzK_n2OTsAhUKWa0KHQauCKkQ9QF6BAgHEAY#imgrc=iYsIHfZTBqNwZM"
["thumbnail"]=>
....
I'm trying to access inline_images with the following PHP code:
$inlineImages = $response['inline_images'];
But whenever I add this line I'm getting a HTTP 500 error. What am I doing wrong? Why can't I see to access inline_images? Commenting the line out makes the page appear properly.
It's a property on an object, so:
$inlineImages = $response->inline_images;
That's an array of objects, so you can loop through them like this:
foreach ($inlineImages as $inlineImage) {
$link = $inlineImage->link;
}
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 3 years ago.
I need to write a foreach using these user's ids:
The array that the API returns:
{"users":[{"id":"14"},{"id":"19"}]}
I want to send a mail based on each user id, thats why I need a foreach statement. How do I do that?
Maybe, here we could first json_decode, then loop through the users and append a username maybe using id values to the array:
$str = '{"users":[{"id":"14"},{"id":"19"}]}';
$array = json_decode($str, true);
foreach ($array["users"] as $key => $value) {
$array["users"][$key]["username"] = "user_" . $value["id"];
}
var_dump($array);
Output
array(1) {
["users"]=>
array(2) {
[0]=>
array(2) {
["id"]=>
string(2) "14"
["username"]=>
string(7) "user_14"
}
[1]=>
array(2) {
["id"]=>
string(2) "19"
["username"]=>
string(7) "user_19"
}
}
}
This question already has answers here:
Return PHP object by index number (not name)
(5 answers)
Closed 7 years ago.
How do I access the following stdclass, and echo Transaction ID has already been used.
object(stdClass)#2 (1) {
["SendBulkSMS_PHPResult"]=>
object(stdClass)#3 (1) {
["string"]=>
array(3) {
[0]=>
string(1) "0"
[1]=>
string(37) "Transaction ID has already been used."
[2]=>
NULL
}
}
}
From the look of it, it would be:
$object->SendBulkSMS_PHPResult->string[1];
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 7 years ago.
I have the following array printed out with var_dump($myArray):
array(2) {
[0]=> object(stdClass)#2 (4) {
["date"]=> string(25) "2015-07-17T05:31:49+02:00"
["author"]=> string(2) "Me"
["subject"]=> string(9) "MySubject"
["message"]=> string(19) "This is my message."
}
["message"]=> string(4) "test"
}
I want to print out only the subject. I tried
echo $myArray[0]["subject"];
but I get an empty site.
Thanks for your help.
As your dump explained ... its an object
so you can access it via echo $myArray[0]->subject;
This question already has answers here:
How to loop through an array of objects that have been decoded from JSON in PHP, and echo the values
(6 answers)
Closed 7 years ago.
I have tried but still not working fine. I have one array that i get from JSON decode in PHP file, I use Ajax for send this array from javascript, this is how i get array.
$q = json_decode($_GET['q'], true);
I do var_dump this variable and this is the result:
array(2)
{ [0]=> array(1)
{ ["data"]=> array(2)
{ ["Text1"]=> string(1) "Car 1" ["Text2"]=> string(1) "Car 2" }
}
[1]=> array(1)
{ ["data"]=> array(2)
{ ["Text1"]=> string(1) "Car 3" ["Text2"]=> string(1) "Car 4" }
}
}
My question is, how do i get value like "Car 1" or "Car 2" etc from this array? this array like 2 dimensional array, i difficult to get this value. i have found many post related it, i try, but still not solved. Really need help please..
try this,
foreach($pass_your_array as $ta)
{
if(isset($ta['data']))
{
foreach ($ta['data'] as $text)
{
$txt1 = $text['Text1']; // gives car1
$txt2 = $text['Text2']; // gives car2
}
}
}