Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I got this working with this customized function. Thanks to Trey for original function at " <a href="https://stackoverflow.com/questions/6553752/array-combine-three-or-more-arrays-with-php?rq=1">array combine three or more arrays with php</a> " You can add as many arrays as you like with this functions.</p> <p>Function #1:</p> <pre><code>function arrayMerge() { $args = func_get_args(); $idx1 = range(1, count($args[1])); $idx2 = range(1, count($args[2])); foreach($args as $key =&gt; $array) { $vkey = array_shift($idx2); foreach($array as $akey =&gt; $val) { $result[$idx1[$akey]][$vkey] = $val; } #foreach } #foreach return $result; } #arrayMerge </code></pre> <p>Example Array Used:</p> <pre><code>$codes = array('US', 'GB', 'AE'); $names = array('United States', 'United Kingdom', 'United Arab Emirates'); $population = array('307,212,123', '61,113,205', '4,798,491'); $area = array('9,826,675', '243,610', '83,600'); </code></pre> <p>Function Usage:</p> <pre><code>print_r( arrayMerge($codes, $names, $population, $area) ); </code></pre> <p>I've added two auto numeric index's and started from 1 instad of 0. </p> <p>Example Array Output:</p> <pre><code>Array ( [1] =&gt; Array ( [1] =&gt; US [2] =&gt; United States [3] =&gt; 307,212,123 [4] =&gt; 9,826,675 ) [2] =&gt; Array ( [1] =&gt; GB [2] =&gt; United Kingdom [3] =&gt; 61,113,205 [4] =&gt; 243,610 ) [3] =&gt; Array ( [1] =&gt; AE [2] =&gt; United Arab Emirates [3] =&gt; 4,798,491 [4] =&gt; 83,600 ) ) </code></pre> <p>Here is one more that uses custom sub-keys, in this one I've added one auto nameric index and started from 1 instead of 0.</p> <p>Function #2:</p> <pre><code>function arrayMerge2() { $args = func_get_args(); $idx = range(1, count($args[1])); $keys = array_shift($args); foreach($args as $key =&gt; $array) { $vkey = array_shift($keys); foreach($array as $akey =&gt; $val) { $result[$idx[$akey]][$vkey] = $val; } #foreach } #foreach return $result; } #arrayMerge2 </code></pre> <p>Example Array Used:</p> <pre><code>$codes = array('US', 'GB', 'AE'); $names = array('United States', 'United Kingdom', 'United Arab Emirates'); $population = array('307,212,123', '61,113,205', '4,798,491'); $area = array('9,826,675', '243,610', '83,600'); $keys = array('code', 'name', 'popu', 'area'); </code></pre> <p>Function Usage:</p> <pre><code>print_r( arrayMerge2($keys, $codes, $names, $population, $area) ); </code></pre> <p>Example Array Output:</p> <pre><code>Array ( [1] =&gt; Array ( [code] =&gt; US [name] =&gt; United States [popu] =&gt; 307,212,123 [area] =&gt; 9,826,675 ) [2] =&gt; Array ( [code] =&gt; GB [name] =&gt; United Kingdom [popu] =&gt; 61,113,205 [area] =&gt; 243,610 ) [3] =&gt; Array ( [code] =&gt; AE [name] =&gt; United Arab Emirates [popu] =&gt; 4,798,491 [area] =&gt; 83,600 ) ) </code></pre> <p><em>Oh yeah, this functions also support empty values in array</em> :-)</p> <p><strong>I hope this helps some one else that needs simular array config.</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload