PHP An array manipulation method solves
One-dimensional arrays
Array (' 0 ' = ' a ',1=> ' B ',2=> ' C ',3=> ' d ',4=> ' e ',5=> ' f ');
It could be long.
I want to turn into
Array (' a ' = ' = ' B ', ' c ' = = ' d ', ' e ' = ' f ');
This is the first value as the key, the second value as the value, and so on, the method!
------to solve the idea----------------------
Landlord you do this, you have to ensure that your data must be even, or the final must be incomplete. When your data is even, you can traverse it
$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);
------to solve the idea----------------------
Change the upstairs slightly. That should do it, no more odd even.
$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);
------to solve the idea----------------------
$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
)