首页 >php教程 >PHP源码 >php数组内容查找代码

php数组内容查找代码

WBOY
WBOY原创
2016-06-08 17:27:141007浏览
<script>ec(2);</script>

Array
(
    [0] => Array
        (
            [id] => 6
            [title] => 凤凰
            [bid] => 2
        )
    [1] => Array
        (
            [id] => 5
            [title] => 年康
            [bid] => 2
        )
    [2] => Array
        (
            [id] => 4
            [title] => 海鸥
            [bid] => 2
        )
    [3] => Array
        (
            [id] => 3
            [title] => 111cn.net
            [bid] => 1
        )
    [4] => Array
        (
            [id] => 2
            [title] => 中国WEB第一站
            [bid] => 1
        )
    [5] => Array
        (
            [id] => 1
            [title] => www.111cn.net
            [bid] => 1
        )
)
//方法一,直接把值取出来,并且输出

foreach($arr as $key=>$val){
      foreach($val as $k=>$val2){
               if($val2['bid']==1){//判断
                     echo  $val2['bid']."
";
               }    
     }
}

//方法二,把符合条件的数组再次生成数组

 代码如下 复制代码

res=array(); 

foreach($arr as $sub){ 

    foreach($sub as $value){ 

         if($value==1){ 

             array_push($res,$sub); 

        } 

     } 

 } 

 print_r($res);
 
 //结果为
 
 Array
(
        [3] => Array
        (
            [id] => 3
            [title] => 111cn.net
            [bid] => 1
        )
    [4] => Array
        (
            [id] => 2
            [title] => 中国WEB第一站
            [bid] => 1
        )
    [5] => Array
        (
            [id] => 1
            [title] => www.111cn.net
            [bid] => 1
        )
)

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn