How to print json non array data [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to print json non array data [duplicate]



This question already has an answer here:



i have JSON data as bellow and want to print "Page" and "PageCount" using php let me know how can i do that.



"response":
"Page": 1,
"PageCount": 1,
"RecordsSent": 1,
"RecordsFound": 1,
"Stock": [

"Colour": "OFF WHITE",
"Size": "S",
"Style": "A0000001",
"Article": "FLORAL 1",
"Size_Range": "S - 3XL",
"In_Stock": 58,
"Demand": 0,
"Supply": 2,
"Sell_Price9": 0

]




how can i print "Page" and "PageCount" only?



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





You convert the JSON into a PHP array and access the appropriate array elements.
– Dormilich
Aug 8 at 11:25




2 Answers
2



You can parse it, like this: Live Demo


<?php

$json = '
"response":
"Page": 1,
"PageCount": 1,
"RecordsSent": 1,
"RecordsFound": 1,
"Stock": [

"Colour": "OFF WHITE",
"Size": "S",
"Style": "A0000001",
"Article": "FLORAL 1",
"Size_Range": "S - 3XL",
"In_Stock": 58,
"Demand": 0,
"Supply": 2,
"Sell_Price9": 0

]


';

$decoded_json = json_decode($json);

echo "Page: ".$decoded_json->response->Page."<br/>";
echo "Page Count: ".$decoded_json->response->PageCount;



You can try this code:


$content = '
"response":
"Page": 1,
"PageCount": 1,
"RecordsSent": 1,
"RecordsFound": 1,
"Stock": [

"Colour": "OFF WHITE",
"Size": "S",
"Style": "A0000001",
"Article": "FLORAL 1",
"Size_Range": "S - 3XL",
"In_Stock": 58,
"Demand": 0,
"Supply": 2,
"Sell_Price9": 0

]

';
$data = json_decode($content, true);

echo "Page:", $data['response']['Page'], "n";
echo "PageCount:", $data['response']['PageCount'];



json_decode second param is


json_decode



Assoc - When TRUE, returned objects will be converted into associative arrays.


Assoc



That means, if assoc = false or not isset, than you have object:


echo "Page:", $data->response->Page, "n";
echo "PageCount:", $data->response->PageCount;





Thank you Vasyl , this method worked for me
– Rajeev Ranjan
Aug 9 at 12:24

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS