Today, on the CSDN, I saw a problem.
One-dimensional arrays
PHP Code
Array (' 0 ' = ' a ',1=> ' B ',2=> ' C ',3=> ' d ',4=> ' e ',5=> ' f ');
May be very long, turn into
Array (' a ' = ' = ' B ', ' c ' = = ' d ', ' e ' = ' f ');
This is the first value as a key, the second value as value, and so on, the less than an even number, and the last null
A good solution:
$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]] =! Empty ($t [1])? $t [1]: null;} Var_dump ($b);
Focus on the Array_chunk () function, the Array_chunk () function splits an array into a new array block.
Example 1
<?php$a=array ("A" = "Cat", "b" = "Dog", "c" = "Horse", "D" and "Cow");p Rint_r (Array_chunk ($a, 2));? >
Output:
Array ([0] = = Array ([0] = Cat [1] = Dog) [1] = = Array ([0] = = Horse [1] = Cow))
Example 2
<?php$a=array ("A" = "Cat", "b" = "Dog", "c" = "Horse", "D" and "Cow");p Rint_r (Array_chunk ($a, 2,true));? >
Output:
Array ([0] = = Array ([a] = Cat [b] = Dog) [1] = = Array ([c] = = Horse [d] = = Cow))
It is easy to see that the third parameter, if you do some projects, you need to return key to 0, you can quickly form
PHP Array_chunk () function