Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP combine two multi-dimensional arrays
    primarykey
    data
    text
    <p>I am trying to use array_combine to combine two multi-dimensional arrays, but somehow not doing it correctly. </p> <p>Here is array1:</p> <pre><code>Array( [Nov 18, 2011] =&gt; Array( [C] =&gt; 107705.5792 [I] =&gt; 44561.52 ) [Nov 22, 2011] =&gt; Array( [C] =&gt; -8992.8352 ) ) </code></pre> <p>and here is array2:</p> <pre><code>Array( [Nov 18, 2011] =&gt; Array( [C] =&gt; 3 [I] =&gt; 1 ) [Nov 22, 2011] =&gt; Array( [C] =&gt; 2 ) ) </code></pre> <p>Here is my attempt at array_combine, which is not working:</p> <pre><code>$array1 = ($arr1); $array2 = ($arr2); $result = array_combine($arr1, $arr2); echo '&lt;pre&gt;'; print_r($result); echo '&lt;/pre&gt;'; </code></pre> <p>What am I doing wrong? This is the result that I am looking for:</p> <pre><code>Array( [Nov 18, 2011] =&gt; Array( [3] =&gt; 107705.5792 [1] =&gt; 44561.52 ) [Nov 22, 2011] =&gt; Array( [2] =&gt; -8992.8352 ) ) </code></pre> <p>Thanks for your help.</p> <ul> <li>EDIT -</li> </ul> <p>I have found that if I instead use array_merge_recursive, this is my the result that I get. Not what I was looking for, but close:</p> <pre><code>Array( [Nov 18, 2011] =&gt; Array( [C] =&gt; Array( [0] =&gt; 3 [1] =&gt; 107705.5792 ) [I] =&gt; Array( [0] =&gt; 1 [1] =&gt; 44561.52 ) ) [Nov 22, 2011] =&gt; Array( [C] =&gt; Array( [0] =&gt; 2 [1] =&gt; -8992.8352 ) ) ) </code></pre> <ul> <li>FURTHER EDIT -</li> </ul> <p>Here is the way that I have tried to implement one of the suggestions below, however this is not working for me. What is wrong?:</p> <pre><code>function cust_array_merge(array &amp;$array1, array $array2){ // loop through main array foreach ($array1 as $key =&gt; $val) { // check if $array2 has the same index if (array_key_exists($key, $array2)) { // reset $array1's indexes to $array2's values foreach ($array2[$key] as $subKey =&gt; $subVal) { if (array_key_exists($subKey, $array1[$key])) { $tempVal = $array1[$key][$subKey]; unset($array1[$key][$subKey]); $array1[$key][$subVal] = $tempVal;}}}}} $merged = cust_array_merge($arr_cats_per_bill_date, $arr_cvat); echo '&lt;pre&gt;'; print_r($merged); echo '&lt;/pre&gt;'; </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