Note that there are some explanatory texts on larger screens.

plurals
  1. POarray combine three or more arrays with php
    primarykey
    data
    text
    <p>ok, assuming I have 5 arrays, all just indexed arrays, and I would like to combine them, this is the best way I can figure, is there a better way to handle this?</p> <pre><code>function mymap_arrays(){ $args=func_get_args(); $key=array_shift($args); return array_combine($key,$args); } $keys=array('u1','u2','u3'); $names=array('Bob','Fred','Joe'); $emails=array('bob@mail.com','fred@mail.com','joe@mail.com'); $ids=array(1,2,3); $u_keys=array_fill(0,count($names),array('name','email','id')); $users=array_combine($keys,array_map('mymap_arrays',$u_keys,$names,$emails,$ids)); </code></pre> <p>this returns:</p> <pre><code>Array ( [u1] =&gt; Array ( [name] =&gt; Bob [email] =&gt; bob@mail.com [id] =&gt; 1 ) [u2] =&gt; Array ( [name] =&gt; Fred [email] =&gt; fred@mail.com [id] =&gt; 2 ) [u3] =&gt; Array ( [name] =&gt; Joe [email] =&gt; joe@mail.com [id] =&gt; 3 ) ) </code></pre> <p>EDIT: After lots of benchmarking I wend with a version of Glass Robots answer to handle a variable number of arrays, it's slower than his obviously, but faster than my original:</p> <pre><code>function test_my_new(){ $args=func_get_args(); $keys=array_shift($args); $vkeys=array_shift($args); $results=array(); foreach($args as $key=&gt;$array){ $vkey=array_shift($vkeys); foreach($array as $akey=&gt;$val){ $result[$keys[$akey]][$vkey]=$val; } } return $result; } $keys=array('u1','u2','u3'); $names=array('Bob','Fred','Joe'); $emails=array('bob@mail.com','fred@mail.com','joe@mail.com'); $ids=array(1,2,3); $vkeys=array('name','email','id'); test_my_new($keys,$vkeys,$names,$emails,$ids); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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