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

php数组内容查找代码

WBOY
WBOYOriginal
2016-06-08 17:27:141007browse
<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
        )
)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn