Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to prevent array_merge to renumber numeric keys
    primarykey
    data
    text
    <p>i have an array which looks like this:</p> <pre><code>Array ( [0] =&gt; Array ( [unit_id] =&gt; 1 [unit_name] =&gt; Clown Fish) [1] =&gt; Array ( [unit_id] =&gt; L [unit_name] =&gt; Liter ) [2] =&gt; Array ( [unit_id] =&gt; 2 [unit_name] =&gt; Elephant ) [3] =&gt; Array ( [unit_id] =&gt; 3 [unit_name] =&gt; Water Bottle ) [4] =&gt; Array ( [unit_id] =&gt; 4 [unit_name] =&gt; Office Seating ) [5] =&gt; Array ( [unit_id] =&gt; 5 [unit_name] =&gt; Green Green Grass ) ) </code></pre> <p>then, i wrote a function</p> <pre><code>function array_to_list($arr_data, $str_key, $str_value) { $arr_list = array(); if (is_array($arr_data)) { foreach($arr_data as $arr_value) { if (isset($arr_value[$str_key]) &amp;&amp; isset($arr_value[$str_value])) { $arr_list = array_merge($arr_list, array($arr_value[$str_key] =&gt; $arr_value[$str_value])); } } } return $arr_list; } </code></pre> <p>to convert the array to look like this</p> <pre><code>Array ( [1] =&gt; Clown Fish ) [L] =&gt; Liter ) [2] =&gt; Elephant ) [3] =&gt; Water Bottle ) [4] =&gt; Office Seating ) [5] =&gt; Green Green Grass ) ) </code></pre> <p>but the output, instead, is</p> <pre><code>Array ( [0] =&gt; Clown Fish ) [L] =&gt; Liter ) [1] =&gt; Elephant ) [2] =&gt; Water Bottle ) [3] =&gt; Office Seating ) [4] =&gt; Green Green Grass ) ) </code></pre> <p>i assume this has something to do with the nature of array_merge itself, which according to php manual "Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array." </p> <p>is there a way to so i can produce the intended result with or without array_merge?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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