douba8819 2013-03-14 12:15
浏览 54
已采纳

搜索数组以获取特定字符串

I am trying to output a specific value from an array, however the value is in a different place each time the array is run, as below:

On one page:

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
                )
}

On another page:

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
                )
}

As you can see, the 'GB' string on one page is at position 0 in the array and, in the other, it is at position 1. Now, the page which this code is being deployed onto is dynamic, so I can't just hard-code $array['countries'][0]['release_date'], where 'release_date' is the actual value I want to pull from the array, so I'm thinking I'd need the code to search through the array for 'GB' (or 'US', or whatever country needs returning), find the index number containing the string and dynamically put that into the query as $uk_release_date, or some such named variable.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • douqiang3768 2013-03-14 12:20
    关注
    $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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献