0

我是 json & php 的新手,我在将 json 转换为 php 字符串时遇到了一些问题

我的 json 字符串看起来像这样

{"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"}
]}

到目前为止,我已经创建了我的字符串

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

解码json

$arr = json_decode($json_raw, TRUE);

我 var_dump($arr);

然后它返回

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" } } } 

现在我想做的就是能够使用这些数据

例如,如果 name = batman then

我知道这是一个愚蠢的问题,但我很挣扎:(

预先感谢

4

3 回答 3

2

json_decode()withTRUE作为第二个参数给你一个关联数组。你需要访问正确的索引来做你想做的事。

要列出具有良好格式的完整关联数组,您可以执行以下操作:

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

现在,要访问name数组中的 :

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

检查它是否Batman(耶!):

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

获取所有相似名称的名称:

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

现场观看!

于 2013-09-22T14:20:56.710 回答
1

当你得到数组时

 $arr = json_decode($json_raw, TRUE);

然后检查卡密钥是否存在

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
    }
  }
}
于 2013-09-22T14:23:42.437 回答
0

尝试:

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

编辑:

好的,所以这对我使用上面的代码有用,如果你想自己试试:

<?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!';
}

?>
于 2013-09-22T14:22:44.453 回答