1

I am using PHP to interact with an API and I am having trouble displaying the value of a sub sub item in a json decoded array that is a response from an API request. I can successfully display all of the array data but I can't display the specific value I am looking for which is the product image link.

I am trying to access 'data->images->link' from the response format below:

{
  "status": 0,
  "country": "string",
  "page_count": 0,
  "page_current": 0,
  "page_size": 0,
  "total_items": 0,
  "data": {
    "code": 0,
    "name": "string",
    "description": "string",
    "categories": [
      "string"
    ],
    "colours": "string",
    "dimensions": [
      "string"
    ],
    "sizing": [
      {
        "sizing_line": "string"
      }
    ],
    "materials": "string",
    "specifications": "string",
    "branding_options": [
     {
       "print_type": "string",
       "print_description": "string"
      }
    ],
    "packaging": "string",
    "carton": {
      "length": 0,
      "width": 0,
      "height": 0,
      "weight": "string",
      "quantity": 0
    },
    "full_colour": 0,
    "mix_and_match": 0,
    "image_count": 0,
    "images": [
      {
        "link": "string",
        "name": "string"
      }
    ],
     "product_wire": "string",
     "pricing": [
        {
        "type": "string",
        "primary_price_description": "string",
        "less_than_moq": "string",
        "prices": [
         {
           "quantity": 0,
           "price": 0
        }
       ],
        "additional_costs": [
          {
            "description": "string",
            "unit_price": 0,
            "setup_price": 0
          }
        ],
        "pricing_comment": "string"
      }
    ]
  }
}

Any help or advice is much appreciated! Thank you :)

0

1 Answer 1

4

If you use json_encode as below then you must need to write in object format:

$test1 = json_decode($test);
print_r($test1->data->images[0]->link);

If you use add "true" as 2nd parametet to json_decode then you can access it as array as below:

$test2 = json_decode($test, true);
print_r($test2['data']['images'][0]['link']);

check this way and let me know if you still have an issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.