数组问题过滤器

时间:2011-04-15 22:07:42

标签: php php-5.3 php-5.2

Array(
[MainArray]=>Array
    (
 [myarray] => Array
        (
            [0] => Array
                (
                    [id] => 1234
                    [url] => google.com
                )

            [1] => Array
                (
                    [id] => 675677
                    [url] => stackoverflow.com
                )

            [2] => Array
                (
                    [id] => 234234
                    [url] => test.com
                )

        )
    )
 ) 

我想要的是选择符合id的网址  因此,如果ID为1234,则网址应为google.com

我有这段代码

 foreach($MainArray['myarray'] as $arr){
            $url = $arr['url'];

}

但它给了我这三个。我需要过滤它们 感谢

1 个答案:

答案 0 :(得分:2)

foreach($MainArray['myarray'] as $arr)
{
  if ($arr['id'] == 1234)
  {
    $url = $arr['url'];
    break;
  }
}