0

I am pulling an array from my DB that is saved in this format:

[categories] => [
    {"category":"Exit Sign"},
    {"category":"Leaving"},
    {"category":"Illuminated"},
    {"category":"Sign"},
    {"category":"Red"},
    {"category":"Warning Sign"},
    {"category":"Above"}
]

How can I loop through each {} and get the category?

Edit: I have attempted passing each JSON array from my DB through json_decode() but I am getting the following error "json_decode() expects parameter 1 to be string, array given..." Any idea what would cause this?

Edit 2: Here is the var_dump output for just one row in my DB:

array(1) {
  ["categories"]=>
  string(845) "[{"category":"Built Structure"},{"category":"The Americas"},{"category":"Sky"},{"category":"New York City"},{"category":"Manhattan - New York City"},{"category":"USA"},{"category":"History"},{"category":"Suspension Bridge"},{"category":"Brooklyn - New York"},{"category":"Brooklyn Bridge"},{"category":"Scenics"},{"category":"Skyscraper"},{"category":"River"},{"category":"Downtown District"},{"category":"East River"},{"category":"Cityscape"},{"category":"Bridge - Man Made Structure"},{"category":"City"},{"category":"Lighting Equipment"},{"category":"Arch"},{"category":"Urban Skyline"},{"category":"Architecture"},{"category":"Sunset"},{"category":"Night"},{"category":"Modern"},{"category":"Urban Scene"},{"category":"Tower"},{"category":"Famous Place"},{"category":"Gate"},{"category":"Outdoors"},{"category":"East"},{"category":"Travel"}]"
}

Got it! I had to pass $array['categories'] into json_decode and then it worked properly. Thanks everyone for your help!

5
  • Can you show us what you've attempted? Commented May 10, 2016 at 15:48
  • could you please use var_dump($your_array_from_db); and post it ? Here you used print_r() and this does not print any info about data type Commented May 10, 2016 at 15:57
  • Updated with what I have attempted, and also a var_dump @Bob0t Commented May 10, 2016 at 16:02
  • @tibsar updated with a var_dump of one lined returned from the DB Commented May 10, 2016 at 16:03
  • Try json_decode($array['categories']) Commented May 10, 2016 at 16:05

1 Answer 1

3

Well, So you are new to this world. You are welcome.

Let your array value is names as $json. This value is a json format, so for access this value you need to decode them. You need to use a function called json_decode, which has two parameter, the first one is string where you pass your variable json, an second one is bool where you pass true or false.

You have to pass when you want your array as associative not object. Here i ignore the second option, so my return array is object.

After decoding your json string you have an array, now you have to use a loop foreach to browse all the elements of the array. As you can see in the resultant array below, the array is multi-dimensional so after applying a foreach loop you just reach the first depth. For getting the value of category you should need to use -> after $val which is the first depth array.

$json = '[{"category":"Exit Sign"},{"category":"Leaving"},{"category":"Illuminated"},{"category":"Sign"},{"category":"Red"},{"category":"Warning Sign"},{"category":"Above"}]';

$arr = json_decode($json); //Also you can pass $yourArr['categories'];

foreach($arr as $val){
    echo $val->category."<br/>";
}

After decoding your array looks like:

Array
(
    [0] => stdClass Object
        (
            [category] => Exit Sign
        )

    [1] => stdClass Object
        (
            [category] => Leaving
        )

    [2] => stdClass Object
        (
            [category] => Illuminated
        )

    [3] => stdClass Object
        (
            [category] => Sign
        )

    [4] => stdClass Object
        (
            [category] => Red
        )

    [5] => stdClass Object
        (
            [category] => Warning Sign
        )

    [6] => stdClass Object
        (
            [category] => Above
        )

)

Result:

Exit Sign
Leaving
Illuminated
Sign
Red
Warning Sign
Above
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply. I've already tried using json_decode() but it returns an error saying "json_decode() expects parameter 1 to be string, array given."
Try passing in the first index of categories
There we go, got it. I had to pass ['categories'] with my array in json_decode and it did the trick... thanks for your help! @tibsar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.