我试图从以下形式的数组中获取数据:
Array ( [actual-1] => 2 [action-1] => blabla [actual-2] => 1 [action-2] => sss [actual-49] => 3 [action-58] => sasa )
ID 1 Level 2 action blabla
ID 2 level 1 action sss
ID 49 level 3 action sasa
我尝试了以下代码:
foreach(array_chunk($array,3,true) as $val){
foreach($val as $k=>$v){
if(strpos($k, "actual") !== false){
$temp = explode("-",$k);
$id = $temp[1];
$actual = $v;
}
if(strpos($k, "action") !== false){
$action = $v;
}
}
echo "ID ".$id." Level ".$actual." action ".$action;
echo "<br>";
}
但我得到的是:
ID 2 Level 1 action blabla
ID 49 level 3 action sasa
答案 0 :(得分:1)
您将数组分块为3个元素而不是2个。将array_chunk中的第二个参数更改为2.
答案 1 :(得分:1)
foreach(array_chunk($array,3,true) as $val){
需要
foreach(array_chunk($array,2,true) as $val){