搜索特定字符串
问题描述:
阵列我从一个阵列试图输出特定的值,但该值在不同的地方每个阵列运行时,如下图所示:搜索特定字符串
在一个页面:
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,在另一方面,它是在位置1。现在,该页面该代码正在被部署o nto是动态的,所以我不能硬编码$array['countries'][0]['release_date']
,其中'release_date'是我想要从数组中拉出的实际值,所以我想我需要代码来搜索'GB '(或'US'或任何需要返回的国家/地区),找到包含该字符串的索引号,并将其作为$ uk_release_date或某些此类指定的变量动态放入查询中。
在此先感谢!
答
$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
+0
谢谢,这是我尝试的第一个解决方案,它完美运行! – 2013-03-14 12:30:34
嗨亚历克斯,看看这个话题:http://*.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php – 2013-03-14 12:19:29