0

Im new to json & php and I'm having some issues with json into php string

My json string looks like this

{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}

So Far i have created my string

$json_raw = '{"status":"OK","cards": [{"id":100001,"name": .....

Decoded the json

$arr = json_decode($json_raw, TRUE);

I var_dump($arr);

then it returns

array(2) { ["status"]=> string(2) "OK" ["cards"]=> array(4) { [0]=> array(8) { ["id"]=> int(100001) ["name"]=> string(6) "batman" ["image"]=> int(11111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T11:37:07Z" } [1]=> array(8) { ["id"]=> int(100002) ["name"]=> string(8) "superman" ["image"]=> int(111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:30:09Z" } [2]=> array(8) { ["id"]=> int(100003) ["name"]=> string(8) "catwoman" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:39:42Z" } [3]=> array(8) { ["id"]=> int(100004) ["name"]=> string(4) "bane" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-09-08T12:56:04Z" } } } 

Now all I want to do is be able to use this data

e.g if name = batman then

I know this is a stupid question but I am struggling :(

Thank in Advance

1
  • 4
    Try this: echo $arr['cards'][0]['name']; (: Commented Sep 22, 2013 at 14:20

3 Answers 3

2

json_decode() with TRUE as second parameter gives you an associative array. You need to access the correct index to do what you want.

To list the complete associative array with nice formatting, you can do:

echo '<pre>', print_r($arr), '</pre>';

Now, to access the name in your array:

$man = $arr['cards'][0]['name'];

To check if it's Batman (yay!):

if( isset($man) && $man == 'batman' ) {
    # code ...
}

For getting the name of all similar names:

$man = $json['cards']['0']['name'];
for ($i=0; $i < count($json['cards']); $i++) { 
    echo $json['cards'][$i]['name']."\n";
}

See it live!

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

1 Comment

if status is not okey then cards key might be empty or not present, so $arr['cards']['0']['name'] it will say undefined
1

when you got the array

 $arr = json_decode($json_raw, TRUE);

then check if cards key exist

if(array_key_exists('cards', $arr)){
  foreach($arr['cards'] as $key=>$val){

  echo $key; ///name, id..
  echo $val; /// batman,...
  if($key == 'name' && $val =='batman'){
      //-------do your stuff
    }
  }
}

Comments

0

Try with:

$cards = $arr['cards'];
foreach($cards as $card) {
    if($card['name'] == 'batman') echo 'Hello batman!';
}

EDIT:

Ok, so this worked for me using code above, try it yourself if you want:

<?php

$json_raw = '{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}';


$arr = json_decode($json_raw, TRUE);

$cards = $arr['cards'];
foreach($cards as $card) {
    if($card['name'] == 'batman') echo 'Hello batman!';
}

?>

6 Comments

It was not me downvoting you, but I suppose it's the fact that the arrays are not nested.
I didn't downvote your answer, but the OP has an associative array and this won't work.
Amal I don't understand why this won't work... I'm doing same as you proposed in a different way. Maybe should I check if isset($card['name'])?
@bestprogrammerintheworld Not nested? But the json string is a multidimensional array right? I'm little confused right now, care to explain the downvotes please so I would know where I'm wrong
@sparkling - I really can't explain why the downvote, because I didn't downvote myself. My suspiscion is though that when talking about nesting arrays, you usally talk about som kind of loops handling arrays in arrays, but mutlidimensional arrays and nested arrays are actually the same thing. Forgive me for confusing you. It's hard to tell when those do downvotes don't actually tell why. That's a shame.
|

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.