如何循环遍历已转换为关联数组的 json 对象以获取 prop 值
How to loop through json object, which has been converted to associative array, to get prop value
我正在使用 PHP(我是菜鸟)从本地服务器检索文件。这是一个 json 文件。然后我将其转换为关联数组。但是,我在遍历属性和数组以定位我需要的值时遇到问题。
下面是数组中每个元素的样子,我试图以每个国家/地区的 iso2 和名称 property/values 为目标。
array (
'type' => 'FeatureCollection',
'features' =>
array (
0 =>
array (
'type' => 'Feature',
'properties' =>
array (
**'name' => 'Bahamas',
'iso_a2' => 'BS',**
'iso_a3' => 'BHS',
'iso_n3' => '044',
),
'geometry' =>
array (
'type' => 'MultiPolygon',
'coordinates' =>
array (
0 =>
array (
0 =>
array (
0 =>
array (
0 => -77.53466,
1 => 23.75975,
),
1 =>
array (
0 => -77.78,
1 => 23.71,
),
2 =>
array (
0 => -78.03405,
1 => 24.28615,
),
3 =>
array (
0 => -78.40848,
1 => 24.57564,
),
4 =>
array (
0 => -78.19087,
1 => 25.2103,
),
5 =>
array (
0 => -77.89,
1 => 25.17,
),
6 =>
array (
0 => -77.54,
1 => 24.34,
),
7 =>
array (
0 => -77.53466,
1 => 23.75975,
),
),
),
1 =>
array (
0 =>
array (
0 =>
array (
0 => -77.82,
1 => 26.58,
),
1 =>
array (
0 => -78.91,
1 => 26.42,
),
2 =>
array (
0 => -78.98,
1 => 26.79,
),
3 =>
array (
0 => -78.51,
1 => 26.87,
),
4 =>
array (
0 => -77.85,
1 => 26.84,
),
5 =>
array (
0 => -77.82,
1 => 26.58,
),
),
),
2 =>
array (
0 =>
array (
0 =>
array (
0 => -77,
1 => 26.59,
),
1 =>
array (
0 => -77.17255,
1 => 25.87918,
),
2 =>
array (
0 => -77.35641,
1 => 26.00735,
),
3 =>
array (
0 => -77.34,
1 => 26.53,
),
4 =>
array (
0 => -77.78802,
1 => 26.92516,
),
5 =>
array (
0 => -77.79,
1 => 27.04,
),
6 =>
array (
0 => -77,
1 => 26.59,
),
),
),
),
),
),
这是我尝试访问数组的方式,但无济于事
forEach($assArr["features"] as $element){
if ($element[0]["properties"]["iso_a2"] == "BS"){
echo_r($element[0]);
}
}
页面输出的内容
“未定义的偏移量:0”
$element
将引用内部的值。
forEach($assArr["features"] as $element){
if ($element["properties"]["iso_a2"] == "BS"){
echo_r($element);
}
}
如果您需要0,可以执行以下操作
forEach($assArr["features"] as $key => $element){
if ($element["properties"]["iso_a2"] == "BS"){
echo_r($key); // will be 0
echo_r($element);
}
}
我正在使用 PHP(我是菜鸟)从本地服务器检索文件。这是一个 json 文件。然后我将其转换为关联数组。但是,我在遍历属性和数组以定位我需要的值时遇到问题。
下面是数组中每个元素的样子,我试图以每个国家/地区的 iso2 和名称 property/values 为目标。
array (
'type' => 'FeatureCollection',
'features' =>
array (
0 =>
array (
'type' => 'Feature',
'properties' =>
array (
**'name' => 'Bahamas',
'iso_a2' => 'BS',**
'iso_a3' => 'BHS',
'iso_n3' => '044',
),
'geometry' =>
array (
'type' => 'MultiPolygon',
'coordinates' =>
array (
0 =>
array (
0 =>
array (
0 =>
array (
0 => -77.53466,
1 => 23.75975,
),
1 =>
array (
0 => -77.78,
1 => 23.71,
),
2 =>
array (
0 => -78.03405,
1 => 24.28615,
),
3 =>
array (
0 => -78.40848,
1 => 24.57564,
),
4 =>
array (
0 => -78.19087,
1 => 25.2103,
),
5 =>
array (
0 => -77.89,
1 => 25.17,
),
6 =>
array (
0 => -77.54,
1 => 24.34,
),
7 =>
array (
0 => -77.53466,
1 => 23.75975,
),
),
),
1 =>
array (
0 =>
array (
0 =>
array (
0 => -77.82,
1 => 26.58,
),
1 =>
array (
0 => -78.91,
1 => 26.42,
),
2 =>
array (
0 => -78.98,
1 => 26.79,
),
3 =>
array (
0 => -78.51,
1 => 26.87,
),
4 =>
array (
0 => -77.85,
1 => 26.84,
),
5 =>
array (
0 => -77.82,
1 => 26.58,
),
),
),
2 =>
array (
0 =>
array (
0 =>
array (
0 => -77,
1 => 26.59,
),
1 =>
array (
0 => -77.17255,
1 => 25.87918,
),
2 =>
array (
0 => -77.35641,
1 => 26.00735,
),
3 =>
array (
0 => -77.34,
1 => 26.53,
),
4 =>
array (
0 => -77.78802,
1 => 26.92516,
),
5 =>
array (
0 => -77.79,
1 => 27.04,
),
6 =>
array (
0 => -77,
1 => 26.59,
),
),
),
),
),
),
这是我尝试访问数组的方式,但无济于事
forEach($assArr["features"] as $element){
if ($element[0]["properties"]["iso_a2"] == "BS"){
echo_r($element[0]);
}
}
页面输出的内容 “未定义的偏移量:0”
$element
将引用内部的值。
forEach($assArr["features"] as $element){
if ($element["properties"]["iso_a2"] == "BS"){
echo_r($element);
}
}
如果您需要0,可以执行以下操作
forEach($assArr["features"] as $key => $element){
if ($element["properties"]["iso_a2"] == "BS"){
echo_r($key); // will be 0
echo_r($element);
}
}