sqlite fetchArray into another array
<p>was having an issue when trying to return a multi array using sqlite over multiple rows.</p>
<p><a href="https://stackoverflow.com/questions/22745546/how-to-make-sqlite3-fetched-array-more-clean" target="blank" rel="noopener">Stack Overflow Inspiration</a> </p>
<p>I tried a few of these but it was not incrementing key, and also it was only showing one result. </p>
<h4>PHP</h4>
<pre><code class="php hljs"> while($row = $result->fetchArray()) {<br /> $hasresults = true;<br /> $returnarray = array();<br /> if(isset($returnasarray) && ($returnasarray == true)) {<br /> / add to an array and return it. /<br /> / each item needs its own sub array. / $key = 1;<br /> $returnarray[$row['id']] = $row;<br /> / $returnarray[$key][$row['id']] = $row; /<br /> / $returnarray[$key] = $returnarray[$row['id']] = $row; /<br /> $key = $key + 1;<br /> // echo $key; /<br /> foreach($this->loadarray as $loadtitle) {<br /> $template->set($loadtitle, $row[$loadtitle]);<br /> $returnarray->push();<br /> $loadvalue = $row[$loadtitle];<br /> $loadvalue = htmlentities($loadvalue);<br /> $returnarray = [$key => ['$loadtitle',$loadvalue]];<br /> $key++;<br /> }<br /> /</code></pre>
<p>Yep that is a mess, so try again. </p>
<p>Ok try number two, this one seems to work, but does not extract all items just specified ones. </p>
<h4>PHP</h4>
<pre><code class="php hljs"> $result = $this->db->query($sql);<br /> $row = array();<br /> $i = 0;<br /> while($res = $result->fetchArray()) {<br /> $row[$i]['id'] = $res['id'];<br /> $row[$i]['title'] = $res['title'];<br /> $i++;<br /> }<br /> return $row</code></pre>
<p>I almost gave up on this one, but this is now returning this array so yay,...</p>
<p><img src="https://i.imgur.com/75MYpjT.png" /></p>
<p>Now that is working i have to see if i can auto load all items in the load array into that array output so i dont have to manually add each one per class. </p>
<p>Now i am happy, its working with the load array for all items required per class extend. </p>
<h4>PHP</h4>
<pre><code class="php hljs"> $result = $this->db->query($sql);<br /> $row = array();<br /> $i = 0;<br /> while($res = $result->fetchArray()) {<br /> foreach($this->loadarray as $loadtitle) {<br /> $row[$i][$loadtitle] = $res[$load_title];<br /> }<br /> $i++;<br /> }<br /> return $row;</code></pre>
<p>Look at that beautiful JSON array.</p>
<p><img style="display: block; margin-left: auto; margin-right: auto;" src="https://i.imgur.com/pc7Am0w.png" /></p>
