问题:
$arr1 = ['name','php','birth','1995','latest','1.0'];
$arr2 = ['name'=>'php','birth'=>'1995','latest'=>'1.0'];
封装一个方法,可以把数组1变成数组2;
一:
function test($arr1){
if (count($arr1) % 2 != 0) {
return; //数组个数为奇数则返回
}
for ($i=0; $i < count($arr1); $i += 2) {
$arr2[$arr1[$i]] = $arr1[$i+1];
}
return $arr2;
}
二:
function ccc($arr1) {
$arr2 = [];
foreach ($arr1 as $key => $value) {
if ($key % 2 == 0) {
if (isset($arr1[$key + 1])) {
$next = $arr1[$key + 1];
} else {
$next = '';
}
$arr2[$value] = $next;
}
}
dump($arr2);
}