我正在尝试从数组中输出一个特定的值,但是每次运行数组时该值都在不同的位置,如下所示:
在一页上:
Array
(
[id] => 12445
[countries] => Array
(
[0] => Array
(
[iso_3166_1] => GB
[certification] => 12A
[release_date] => 2011-07-07
)
[1] => Array
(
[iso_3166_1] => US
[certification] => PG-13
[release_date] => 2011-07-15
)
[2] => Array
(
[iso_3166_1] => DE
[certification] => 12
[release_date] => 2011-07-12
)
}
在另一页上:
Array
(
[id] => 673
[countries] => Array
(
[0] => Array
(
[iso_3166_1] => US
[certification] => PG
[release_date] => 2004-06-04
)
[1] => Array
(
[iso_3166_1] => GB
[certification] => PG
[release_date] => 2004-05-31
)
[2] => Array
(
[iso_3166_1] => IT
[certification] => T
[release_date] => 2004-06-04
)
}
正如您所看到的,一个页面上的“GB”字符串位于数组中的位置0,而另一个页面中的“GB”字符串位于位置1.现在,此代码所部署的页面是动态的,因此我不能只硬编码$array['countries'][0]['release_date']
,其中'release_date'是我想从数组中提取的实际值,所以我想我需要代码在数组中搜索'GB' (或'US',或任何需要返回的国家/地区),找到包含字符串的索引号,并将其作为$ uk_release_date或某些此类命名变量动态地放入查询中。
提前致谢!
答案 0 :(得分:1)
$index = -1;
foreach($array['countries'] as $k=>$v) {
if(array_search('GB', $v)) { // Search for GB
$index = $k;
break;
}
}
echo $array['countries'][$index]['release_date']; // This will be the release date for GB