Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I change three-dimensional array to single-dimentional array(PHP)
    primarykey
    data
    text
    <p>I have a three dimensional array. I want to right a function to transform it to a single dimensional array and keep the previous sub-key. How do I do that?</p> <pre><code>array(2) { [0]=&gt; array(2) { [0]=&gt; array(2) { ["id"]=&gt; string(1) "4" ["assignedlessons"]=&gt; string(2) "69" } [1]=&gt; array(2) { ["id"]=&gt; string(1) "4" ["assignedlessons"]=&gt; string(3) "308" } } array(2) { [0]=&gt; array(2) { ["id"]=&gt; string(1) "10" ["assignedlessons"]=&gt; string(2) "50" } [1]=&gt; array(2) { ["id"]=&gt; string(1) "10" ["assignedlessons"]=&gt; string(2) "91" } } } </code></pre> <p>I want to transform it to the structure like this one:</p> <pre><code> array(0) { ["id"]=&gt; string(1) "4" ["assignedlessons"]=&gt; string(2) "69" } array(1) { ["id"]=&gt; string(1) "4" ["assignedlessons"]=&gt; string(3) "308" } array(2) { ["id"]=&gt; string(1) "10" ["assignedlessons"]=&gt; string(2) "50" } array(3) { ["id"]=&gt; string(1) "10" ["assignedlessons"]=&gt; string(2) "91" } </code></pre> <p>What I have is this function:</p> <pre><code>function toSingleDimentionalArray($array){ $final = array(); foreach($array as $value){ foreach($array as $key =&gt; $val){ $final[]['id'] = $val['id']; $final[]['assignedlessons'] = $val['assignedlessons']; } } return $final; } </code></pre> <p>But it only return something like this:</p> <pre><code>array(19648) { [0]=&gt; array(1) { ["id"]=&gt; string(1) "4" } [1]=&gt; array(1) { ["assignedlessons"]=&gt; string(2) "69" } [2]=&gt; array(1) { ["id"]=&gt; string(1) "4" } [3]=&gt; array(1) { ["assignedlessons"]=&gt; string(3) "308" } </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.
    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