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?

1
  • 2
    You convert the JSON into a PHP array and access the appropriate array elements. Commented Aug 8, 2018 at 11:25

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Vasyl , this method worked for me
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;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.