0

我正在尝试从数组中输出特定值,但是每次运行数组时该值都在不同的位置,如下所示:

在一页上:

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。现在,此代码部署到的页面是动态的,所以我可以t just hard-code $array['countries'][0]['release_date'],其中'release_date'是我想从数组中提取的实际值,所以我想我需要代码来搜索数组中的'GB'(或'US',或其他country 需要返回),找到包含该字符串的索引号并将其作为 $uk_release_date 或某些此类命名变量动态地放入查询中。

提前致谢!

4

1 回答 1

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
于 2013-03-14T12:20:38.227 回答