php-如何解析单个json对象数组

时间:2018-04-28 14:48:57

标签: php json parsing

[{"CUSTOMERCODE":"9879","CUSTNAME":"ASHIQUE","MOBILE":"","NATION":"INDIA","AREA":"AL QUOZ"},{"CUSTOMERCODE":"5678","CUSTNAME":"SALIH","MOBILE":"971528381946","NATION":"UAE","AREA":"DEIRA"},{"CUSTOMERCODE":"2345","CUSTNAME":"HABEEB","MOBILE":"12345678","NATION":"SAUDI","AREA":"AL QOUZ 4"},{"CUSTOMERCODE":"5433","CUSTNAME":"AZHAR","MOBILE":"12345678","NATION":"USA","AREA":"AL QOUZ 3"},{"CUSTOMERCODE":"5698","CUSTNAME":"BAIJU","MOBILE":"12345678","NATION":"UK","AREA":"AL KHAIL"},{"CUSTOMERCODE":"3854","CUSTNAME":"RAHUL","MOBILE":"12345678","NATION":"KUWAIT","AREA":"AL QUOZ"},{"CUSTOMERCODE":"5623","CUSTNAME":"LINESH","MOBILE":"12345678","NATION":"UAE","AREA":"AL KHAIL"},{"CUSTOMERCODE":"9879","CUSTNAME":"ASHIQUE","MOBILE":"12345678","NATION":"INDIA","AREA":"AL QUOZ"},{"CUSTOMERCODE":"5678","CUSTNAME":"SALIH","MOBILE":"12345678","NATION":"UAE","AREA":"DEIRA"},{"CUSTOMERCODE":"2345","CUSTNAME":"HABEEB","MOBILE":"12345678","NATION":"SAUDI","AREA":"AL QOUZ 4"},{"CUSTOMERCODE":"5433","CUSTNAME":"AZHAR","MOBILE":"12345678","NATION":"USA","AREA":"AL QOUZ 3"},{"CUSTOMERCODE":"5698","CUSTNAME":"BAIJU","MOBILE":"12345678","NATION":"UK","AREA":"AL KHAIL"},{"CUSTOMERCODE":"3854","CUSTNAME":"RAHUL","MOBILE":"971557557863","NATION":"KUWAIT","AREA":"AL QUOZ"},{"CUSTOMERCODE":"5623","CUSTNAME":"LINESH","MOBILE":"12345678","NATION":"UAE","AREA":"AL KHAIL"}]

如何在循环中打印每个CUSTNAME?

2 个答案:

答案 0 :(得分:1)

一旦你有json_decode结果返回一个正确的关联数组

$myJson = json_decode($json, true); 

你可以在数组中迭代一个元素数组,例如:使用echo显示值

foreach ($myJson as $key => $value){
      echo $value['CUSTNAME'] . '<br>';
}

如果想使用for循环

$cnt = count($myJson);

for ($i = 0; $i<$cnt; $i++){
    echo $myJson[$i]['CUSTNAME'] . '<br>';
}

答案 1 :(得分:0)

您可以json_decode json并使用array_column方法获取每个CUSTNAME键值。

$json = '[{"CUSTOMERCODE":"9879","CUSTNAME":"ASHIQUE","MOBILE":"","NATION":"INDIA","AREA":"AL QUOZ"},{"CUSTOMERCODE":"5678","CUSTNAME":"SALIH","MOBILE":"971528381946","NATION":"UAE","AREA":"DEIRA"},{"CUSTOMERCODE":"2345","CUSTNAME":"HABEEB","MOBILE":"12345678","NATION":"SAUDI","AREA":"AL QOUZ 4"},{"CUSTOMERCODE":"5433","CUSTNAME":"AZHAR","MOBILE":"12345678","NATION":"USA","AREA":"AL QOUZ 3"},{"CUSTOMERCODE":"5698","CUSTNAME":"BAIJU","MOBILE":"12345678","NATION":"UK","AREA":"AL KHAIL"},{"CUSTOMERCODE":"3854","CUSTNAME":"RAHUL","MOBILE":"12345678","NATION":"KUWAIT","AREA":"AL QUOZ"},{"CUSTOMERCODE":"5623","CUSTNAME":"LINESH","MOBILE":"12345678","NATION":"UAE","AREA":"AL KHAIL"},{"CUSTOMERCODE":"9879","CUSTNAME":"ASHIQUE","MOBILE":"12345678","NATION":"INDIA","AREA":"AL QUOZ"},{"CUSTOMERCODE":"5678","CUSTNAME":"SALIH","MOBILE":"12345678","NATION":"UAE","AREA":"DEIRA"},{"CUSTOMERCODE":"2345","CUSTNAME":"HABEEB","MOBILE":"12345678","NATION":"SAUDI","AREA":"AL QOUZ 4"},{"CUSTOMERCODE":"5433","CUSTNAME":"AZHAR","MOBILE":"12345678","NATION":"USA","AREA":"AL QOUZ 3"},{"CUSTOMERCODE":"5698","CUSTNAME":"BAIJU","MOBILE":"12345678","NATION":"UK","AREA":"AL KHAIL"},{"CUSTOMERCODE":"3854","CUSTNAME":"RAHUL","MOBILE":"971557557863","NATION":"KUWAIT","AREA":"AL QUOZ"},{"CUSTOMERCODE":"5623","CUSTNAME":"LINESH","MOBILE":"12345678","NATION":"UAE","AREA":"AL KHAIL"}]';

$data = json_decode($json);
echo "<pre>";
print_r(array_column($data, 'CUSTNAME'));