时间:2021-07-01 10:21:17 帮助过:23人阅读
array('0'=>'a',1=>'b',2=>'c',3=>'d',4=>'e',5=>'f');
array('a'=>'b','c'=>'d','e'=>'f');
$arr = array('0'=>'a',1=>'b',2=>'c',3=>'d',4=>'e',5=>'f','6'=>'g','7'=>'h');
$i = 0;
$len = count($arr);
$newArr = array();
while($i < $len){
$newArr[$arr[$i]] = $arr[$i+1];
$i += 2;
}
var_dump($newArr);
?>
$arr = array('0'=>'a',1=>'b',2=>'c',3=>'d',4=>'e',5=>'f','6'=>'g','7'=>'h','8'=>'i');
$i = 0;
$len = count($arr);
$newArr = array();
while($i < $len){
$newArr[$arr[$i]] = array_key_exists($i+1,$arr)?$arr[$i+1]:null;
$i += 2;
}
var_dump($newArr);
?>
$a = array('0'=>'a',1=>'b',2=>'c',3=>'d',4=>'e',5=>'f', 'G');
foreach(array_chunk($a, 2) as $t) {
$b[$t[0]] = @$t[1];
}
print_r($b);
Array
(
[a] => b
[c] => d
[e] => f
[G] =>
)