Home > Article > Backend Development > PHP 一个数组操作方法求解解决方案
PHP 一个数组操作方法求解
一维数组
<br />array('0'=>'a',1=>'b',2=>'c',3=>'d',4=>'e',5=>'f');<br />
<br />array('a'=>'b','c'=>'d','e'=>'f');<br />
<br /><?php <br />$arr = array('0'=>'a',1=>'b',2=>'c',3=>'d',4=>'e',5=>'f','6'=>'g','7'=>'h');<br />$i = 0;<br />$len = count($arr);<br />$newArr = array();<br />while($i < $len){<br /> $newArr[$arr[$i]] = $arr[$i+1];<br /> $i += 2;<br />}<br />var_dump($newArr);<br />?> <br />
<br /><?php <br />$arr = array('0'=>'a',1=>'b',2=>'c',3=>'d',4=>'e',5=>'f','6'=>'g','7'=>'h','8'=>'i');<br />$i = 0;<br />$len = count($arr);<br />$newArr = array();<br />while($i < $len){<br /> $newArr[$arr[$i]] = array_key_exists($i+1,$arr)?$arr[$i+1]:null;<br /> $i += 2;<br />}<br />var_dump($newArr);<br />?> <br />
$a = array('0'=>'a',1=>'b',2=>'c',3=>'d',4=>'e',5=>'f', 'G');<br />foreach(array_chunk($a, 2) as $t) {<br /> $b[$t[0]] = @$t[1];<br />}<br />print_r($b);<br /><br />
Array<br />(<br /> [a] => b<br /> [c] => d<br /> [e] => f<br /> [G] => <br />)<br /><br />