Two original arrays
$arr [' id '] = array ([0] = ' 1 ', [1] = = false,[2] = ' 3 ', [3] = ' 4 ',) $arr [' name '] = Array ([0] = ' Zhang ', [1] = > ' Li ', [2] = ' du ',)
The array that needs to be generated
Array ([0]=>array (' id ' = ' = ' 1 ', ' name ' + ' Zhang '), [1]=>array (' id ' =>false, ' name ' = = '), [2]=>array (' id ' = ' 3 ', ' name ' = ' li '), [3]=>array (' id ' = ' 4 ', ' name ' = ' du '),)
Reply content:
Two original arrays
$arr [' id '] = array ([0] = ' 1 ', [1] = = false,[2] = ' 3 ', [3] = ' 4 ',) $arr [' name '] = Array ([0] = ' Zhang ', [1] = > ' Li ', [2] = ' du ',)
The array that needs to be generated
Array ([0]=>array (' id ' = ' = ' 1 ', ' name ' + ' Zhang '), [1]=>array (' id ' =>false, ' name ' = = '), [2]=>array (' id ' = ' 3 ', ' name ' = ' li '), [3]=>array (' id ' = ' 4 ', ' name ' = ' du '),)
I generally understand that I do not know the right ...
The first decimal group in a large array is always the longest ... If there is a value in this array false
then complement the rest as blank ...
Write a snippet example code as follows ... Not only can it be applied to your situation ... The source array is more complex and can be processed ...
[' 1 ', false, ' 3 ', ' 4 '], ' name ' = = [' Zhang ', ' Li ', ' du ']];/* get all key names ... */$keys = Array_keys ($arr );/* Use a different-solve-*/var_export (array_reduce ($arr, function (& $result, $current) use (& $keys) {/* Get current Key name ... */$key = Array_shift ($keys); /* First run ...? */if (! Isset ($result))/* Initialize a new array with primary key ... */$result = Array_map (functi On ($value) use ($key) {/* Just packing value as an array ... */return [$key = = $value]; /* value based on $current ... known as first entry of $arr ... */}, $current); /* When the array is built ... add new keys into it ... */else foreach ($result as $num = & $value)/ * Leave blank when primary key was false or entry is empty ... */$value [$key] = (false!== current ($value))? Array_shift ($current)?: ': '; /* Send the new ARray back ... */return $result;} ) );
The principle is to establish a primary key based on the first decimal group in the original array ... Then all elements are pieced together according to the primary key ...
It doesn't matter what each element is called ... It doesn't matter how many elements ... The key values of other decimal groups other than the primary key array are completely unimportant ...
In short, they are spelled together in order to become another large array.
and say ... If the length of the decimal group exceeds the length of the primary key array ... The extra-long part will be discarded ...
If the primary key array is longer than the decimal group ... The vacant part will also fill in the blanks ...
Basically that's it ... Well, actually ... You can also simply use a For loop to complete ... Higher efficiency ...
But with Array_reduce () More handsome adaptability is also stronger not.
$rev = Array (), $ix = 0;foreach ($arr [' id '] as $k = = $v) { $rev [] = array (' id ' = * = $v , ' name ' = = $v? $arr " Name '] [$ix + +]: ' );} Print_r ($rev);
Upstairs to explain and very beautiful code, below I give a simple implementation.
$arr = array (' id ' = = Array (' 1 ', False, ' 3 ', ' 4 '), ' name ' = = Array (' Zhang ', ' Li ', ' du ') ), foreach ($arr [' id '] as $value) {if (Intval ($value) > 0) {$result [] = Array ($value = = Array_shift ($arr [' name ']);} else {$result [] = Array ($value = =);}} Var_dump ($result);
Wouldn't it be nice to use array_combine?