充分利用数组中的元素与德code JSON内容 [英] Getting elements from an array with contents from decode json

查看:145
本文介绍了充分利用数组中的元素与德code JSON内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点很难形容在标题的问题,但我会在身体解释。所以我有这个例子JSON(它类似于一个我):

It's sort of hard to describe the question in the title, but I will explain it in the body. So I have this example JSON (It's similar to the one I have):

[{"AssetId":234234,"Name":"Test1"},{"AssetId":53453,"Name":"Test2"}]

所以基本上我想从主阵列的第一阵列获得由assetid元素。我打电话这PHP,但它不工作

So basically I want to get the AssetId element from the first array of the main array. I am calling this in PHP, but it isn't working

$array = json_decode("[{"AssetId":234234,"Name":"Test1"},{"AssetId":53453,"Name":"Test2"}]");
echo $array[0][0]

所以,我想这将是主要的数组中的第一个数组以得到与Test1的名称的元素由assetid。得到它的帮助?

So I guess that would be the first array in the main array to get AssetId of the element with the Name of Test1. Any help on getting it?

推荐答案

尝试更换单引号的双引号,你的企图将导致意外解析的T_STRING误差

Try replacing the double quotes with single quotes, your attempt will lead to parse error of unexpected T_STRING

取而代之的是

$array = json_decode("[{"AssetId":234234,"Name":"Test1"},{"AssetId":53453,"Name":"Test2"}]");
                     ^        

试试这个:

$array = json_decode('[{"AssetId":234234,"Name":"Test1"},{"AssetId":53453,"Name":"Test2"}]');
foreach($array as $arr){
  echo $arr->AssetId;
}

这篇关于充分利用数组中的元素与德code JSON内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆