在for循环中执行一次值

在具有2次的数组列表[hide] => 1中,如何仅在for循环中执行一次[hide] => 1。如何用所有先前的值检查当前数组并且[hide] => 1在for循环中执行一次


需要执行[id] => 4,无需在for循环中执行[id] => 2


大批


Array ( 

    [0] => Array ( [id] => 6 [hide] => 0  ) 

    [1] => Array ( [id] => 5  [hide] => 0 )

    [2] => Array ( [id] => 4  [hide] => 1 )

    [3] => Array ( [id] => 3  [hide] => 0  )

    [4] => Array ( [id] => 2 [hide] => 1  )

)


胡子哥哥
浏览 190回答 3
3回答

拉丁的传说

我想您想要ID最大的商品:// get only the items with 'hide' = 1$hidden = array_filter($array, function($item){return $item['hide'] == 1;});// order the array to have the items with the greatest ID firstusort($hidden, function($a, $b){    return $b['id'] - $a['id'] ;});// print the item with the max id print_r($hidden[0]);

12345678_0001

尝试这个....。您可以对关联数组的任何深度使用此功能。 function is_in_array($array, $key, $key_value){    $within_array = 'no';    foreach( $array as $k=>$v ){      if( is_array($v) ){          $within_array = is_in_array($v, $key, $key_value);          if( $within_array == 'yes' ){              break;         }      } else {             if( $v == $key_value && $k == $key ){                     $within_array = 'yes';                     break;             }        }     }      return $within_array;   }   print_r(is_in_array($yourarray, 'hide', '1'));

慕桂英3389331

for($i = 0; $i<count($array); $i++){&nbsp; if($array[$i] == 4){&nbsp; &nbsp; &nbsp; &nbsp;print_r($array[4]);&nbsp; &nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP