Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
foreach-PHP中作为变量的数组键_Php - Fatal编程技术网

foreach-PHP中作为变量的数组键

foreach-PHP中作为变量的数组键,php,Php,这可能很简单。。。但我想不出来。我需要使用数组键作为变量。我有三个选项卡,需要使用唯一变量来访问数据 $array = array( 'items' => 'latest', 'itemsFollow' => 'follow', 'itemsExp' => 'expired' ); while ( ($stuff = current($array)) !== FALSE ) { echo '<div id="'.key($array).'"

这可能很简单。。。但我想不出来。我需要使用数组键作为变量。我有三个选项卡,需要使用唯一变量来访问数据

$array = array(
    'items' => 'latest',
    'itemsFollow' => 'follow',
    'itemsExp' => 'expired'
);

while ( ($stuff = current($array)) !== FALSE ) {
  echo '<div id="'.key($array).'" class="tab-content grid flex">';

    foreach(/*array_key*/ as $item) { //need foreach($items, foreach($itemsFollow and foreach($itemsExp
    // do stuff
    }

  echo '</div>';

  next($array);
}
$array=array(
'项目'=>'最新',
'itemsFollow'=>'follow',
'itemsExp'=>'已过期'
);
while(($stuff=current($array))!==FALSE){
回声';
foreach(/*array\u key*/as$item){//需要foreach($items,foreach($itemsFollow)和foreach($itemsep)
//做事
}
回声';
下一个($数组);
}

使用foreach在数组中循环。无需在一维数组中循环两次(嵌套循环)。使用
if、elseif
case
语句根据值执行自定义操作

foreach ($array as $key => $value){
     echo '<div id="'.$key.'" class="tab-content grid flex">';

     if($value=="latest"){
       //do stuff
     }
     elseif($value=="follow"){
       //do stuff
     }
     elseif($value=="expired"){
       //do stuff
     }

}
foreach($key=>$value的数组){
回声';
如果($value==“最新”){
//做事
}
elseif($value==“follow”){
//做事
}
elseif($value==“过期”){
//做事
}
}
foreach($k=>v的数组)