Heim  >  Artikel  >  php教程  >  php数组内容查找代码

php数组内容查找代码

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn