Php 在for循环中执行一次值

Php 在for循环中执行一次值,php,Php,在有2次的数组列表[hide]=>1中,如何在for循环中只执行一次[hide]=>1。如何使用所有以前的值检查当前数组,并在for循环中执行一次[hide]=>1 需要执行[id]=>4,无需在for循环中执行[id]=>2 排列 对于($i=0;$i请尝试此…。您可以对关联数组的任何深度使用此函数 function is_in_array($array, $key, $key_value){ $within_array = 'no'; foreach( $array as

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

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

排列


对于($i=0;$i请尝试此…。您可以对关联数组的任何深度使用此函数

 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'));

我想您想要具有最大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 first
usort($hidden, function($a, $b){
    return $b['id'] - $a['id'] ;
});

// print the item with the max id 
print_r($hidden[0]);

我上面的代码是一个示例,我在for循环中有一个动态数组,你的答案不适合于此,请添加一些说明。你说的“执行数组值”是什么意思?请重新格式化,这总体上没有多大意义(至少对我来说)。试着使用
array\u filter()
获取数组子集以执行您想要的操作。tbh我真的不确定您想要什么只是使用一个简单的布尔标志…?
 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'));
// 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 first
usort($hidden, function($a, $b){
    return $b['id'] - $a['id'] ;
});

// print the item with the max id 
print_r($hidden[0]);