需要语法来访问json对象中的数组

时间:2016-10-07 14:27:46

标签: php json

我从网络服务收到以下输出。 (显示的输出是通过var_dump(json_decode($json,true));从我可以使用的对象的后半部分获取数据,例如:

$obj = json_decode($json);
$myvalue=$obj->{'CurrentAccountNumber'};

但我还需要从对象开头的数组中获取数据(通过php)。使用php获取/回显数组数据的正确语法是什么?谢谢!

从WEB服务输出:

  

array(30){[“Accounts”] => array(2){[0] => array(4){[“AccountNumber”] => string(9)“123456789”[“CurrentBalance”] => float(455.17)[“FullName”] => string(8)“JOHN DOE”[“CodeNum”] => string(3)“A34”} [1] => array(4){[“AccountNumber”] => string(9)“123456788”[“CurrentBalance”] => float(67.22)[“FullName”] => string(8)“JOHN DOE”[“CodeNum”] => string(3)“B82”}}

我如何从这条线以上的阵列获取数据?

  

[ “CurrentAccountNumber”] => string(9)“123456789”[“CurrentBalance”] => float(455.17)[“CustomMonth”] => bool(true)[“CustomMonthAmt”] => float(488.17)[“CustomMonthLength”] => int(0)[“DateOfService”] => string(10)“2013-06-15”[“EighteenMonth”] => bool(false)[“EighteenMonthAmt”] => int(0)[“ExpectedLastPaymentAmount”] => float(0)[“ExpectedLastPaymentDate”] => string(0)“”[“FullName”] => string(8)“JOHN DOE”[“Email”] => string(11)“myemail.com”[“LastFourPhone”] => string(4)“5128”[“LastPaymentAmount”] => float(4205.83)[“LastPaymentDate”] => string(10)“2015-04-28”[“NextPaymentAmount”] => float(0)[“NextPaymentDate”] => string(0)“”[“Category”] => int(0)[“InsPlanStartDate”] => string(10)“2016-10-08”[“PrimaryPhoneNumber”] => string(10)“5555555128”[“SixMonth”] => bool(false)[“SixMonthAmt”] => int(0)[“CodeNum”] => string(3)“A34”[“TwelveMonth”] => bool(false)[“TwelveMonthAmt”] => int(0)[“TwentyFourMonth”] => bool(false)[“TwentyFourMonthAmt”] => int(0)[“UseInfoOnFile”] => bool(true)[“ZipCode”] => string(5)“12345”}

2 个答案:

答案 0 :(得分:1)

改为使用$obj->CurrentAccountNumber语法。

如果将json解码为数组而不是对象,也可以将它与典型的数组语法$obj['CurrentAccountNumber']一起使用,就像在var_dump中一样。

要执行此操作,请更改此

$obj = json_decode($json);

由此

$array = json_decode($json, true);

答案 1 :(得分:0)

json_decode($json)StdClass数据中生成JSON个对象(如果您未将第二个参数设置为true,请参阅http://php.net/manual/en/function.json-decode.php ),所以你可以用$obj->propertyName访问它的属性,如果它是一个数组(使用foreach左右),则迭代其他属性。

如果你想保留一个关联数组,保留第二个参数json_decode($json, true);并像$obj['CurrentAccountNumber']一样访问任何其他关联数组的值(但这里,变量名称令人困惑)。

相关问题