-3

I have the following JSON file.... How do I display the value of "items-per-page" in PHP ?

{
   "status":"ok",
   "message-type":"funder-list",
   "message-version":"1.0.0",
   "message":{
      "items-per-page":20,
      "query":{
         "start-index":0,
         "search-terms":null
      },
      "total-results":31230,
      "items":[
         {
            "id":"100000001",
            "location":"United States",

I tried this and it doesn't work:

$resp = curl_exec($curl);
curl_close($curl);
$data = json_decode($resp);
echo $data->message["items-per-page"];

thank you.

Isaac
  • 406
  • 2
  • 8
  • 16
  • To access the property of a property, use another arrow. When a property name has characters that break valid syntax (like hyphens), use curly braces and quotes. `echo $data->message->{"items-per-page"}`. – mickmackusa Jun 04 '22 at 06:10

1 Answers1

-1

Try making it an associative array since the dashes in some of the field names will not be able to be represented in dot notation.

$resp = curl_exec($curl);
curl_close($curl);
$data = json_decode($resp, true); // where true is for associative 
echo $data["message"]["items-per-page"];
mardubbles
  • 129
  • 6
  • We get these questions every day. Stack Overflow does not benefit from having them answered. Please flag/vote to close duplicate questions. [The fundamental goal of closing duplicate questions is to help people find the right answer by getting all of those answers in one place.](https://stackoverflow.com/help/duplicates#:~:text=The%20fundamental%20goal%20of%20closing%20duplicate%20questions%20is%20to%20help%20people%20find%20the%20right%20answer%20by%20getting%20all%20of%20those%20answers%20in%20one%20place.) – mickmackusa Jun 04 '22 at 06:03