Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this:</p> <pre><code>$keys = array_unique(array_merge(array_keys($arr1), array_keys($arr2))); $values = array_map(function($key) use ($arr1, $arr2) { return array('f1' =&gt; isset($arr1[$key]) ? $arr1[$key] : "0", 'f2' =&gt; isset($arr2[$key]) ? $arr2[$key] : "0"); } , $keys); $result = array_combine($keys, $values); var_dump($result); </code></pre> <p>Some explanation:</p> <ul> <li>At first get an array <code>$keys</code> of all unique keys of both arrays.</li> <li>Then for each key the corresponding array of values is then generated.</li> <li>At last the array of keys and the array of values are combined.</li> </ul> <p>But this does currently only work with two arrays.</p> <hr> <p><strong>Edit</strong>    Here’s one that works with an arbitrary number of arrays:</p> <pre><code>$arrays = array($arr1, $arr2); $keys = array_unique(call_user_func_array('array_merge', array_map('array_keys', $arrays))); $values = array_map(function($key) use ($arrays) { return array_combine(array_map(function($key) { return 'f'.($key+1); }, array_keys($arrays)), array_map(function($array) use ($key) { return isset($array[$key]) ? $array[$key] : "0"; }, $arrays)); }, $keys); $result = array_combine($keys, $values); var_dump($result); </code></pre> <p>Some explanation:</p> <ul> <li>Put all arrays in an array <code>$arrays</code>.</li> <li>For each array in <code>$arrays</code>, call <code>array_keys</code> on it to get the keys, merge these arrays and get an array <code>$keys</code> of all unique keys.</li> <li>For each key in <code>$keys</code>, check if the key exists in the array of arrays <code>$arrays</code>.</li> <li>Combine the keys and values.</li> </ul>
    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.
    1. This table or related slice is empty.
    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