0

I have the following JSON data which I'm trying to parse the categories:

{
"skimlinksProductAPI": {
    "status": 200,
    "message": "OK",
    "version": 3,
    "categories": {
        "1": "Animals",
        "2": "Animals > Live Animals",
        "3": "Animals > Pet Supplies",
        "4": "Animals > Pet Supplies > Bird Supplies",
        "5": "Animals > Pet Supplies > Bird Supplies > Bird Cages & Stands",
        "6": "Animals > Pet Supplies > Bird Supplies > Bird Food",
        "7": "Animals > Pet Supplies > Bird Supplies > Bird Ladders & Perches",
        "8": "Animals > Pet Supplies > Bird Supplies > Bird Toys",
        ...
    }
  }
}

I'm trying the following but not working:

$catList = json_decode(file_get_contents('http://api-product.skimlinks.com/categories?key=MY_KEY&format=json'),true);

$catList=$catList['skimlinksProductAPI']['categories'];

foreach ($catList as $element){

    echo $element[0].' - '.$element[1];

}
1
  • did you bother checking json_last_error() and doing var_dump($catList) to see what you're getting back? Commented Mar 31, 2014 at 21:57

1 Answer 1

1

The elements of categories aren't sub-arrays, they're just key-value pairs. The foreach should be:

foreach ($catList as $id => $element){
     echo $id . ' - ' . $element;
}
Sign up to request clarification or add additional context in comments.

2 Comments

That just gives me a blank page unfortunately.
The echo was missing.

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.