1

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?

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40

2 Answers2

1

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

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

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

echo "Page:", $data->response->Page, "\n";
echo "PageCount:", $data->response->PageCount;
Vasyl Zhuryk
  • 1,228
  • 10
  • 23
0

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;
Atlas_Gondal
  • 2,512
  • 2
  • 15
  • 25