php arrays. How to format for my result
<p>I am pretty new to php and could sure use some help understanding how to get my result the way I need it from a database query.</p>

<p>What I need is an associative array like this, 'bla'=>'bla'. What I am getting from my foreach loop is this from a print:</p>

<pre><code>[0] =&gt; Array
    (
        [0] =&gt; test0
        [name] =&gt; test0
        [1] =&gt; 1
        [customer_id] =&gt; 1
    )

[1] =&gt; Array
    (
        [0] =&gt; test
        [name] =&gt; test
        [1] =&gt; 2
        [customer_id] =&gt; 2
    )
</code></pre>

<p>Here is my loop:</p>

<pre><code>foreach($res as $key=&gt;$val)
{
  // have no idea..
}
</code></pre>

<p>Can someone please help me to format my results so that they are like 'index'=>'value'</p>

<p>Thanks for any help.</p>

<p><hr /></p>

<p>Here is a sample code that uses a foreach but yet pulls an association. I don't get it. I am thinking that my result set with the indexes are because I am not writing the loop correctly. Here is the code that uses the foreach</p>

<pre><code>foreach ($items as $key=&gt;$value) {
    if (strpos(strtolower($key), $q) !== false) {
     echo "$key|$value\n";
    }
}
</code></pre>

<p><hr /></p>

<p>Here is the part of the database class that I am using to fetch the results.</p>

<pre><code>$returnArray = array();
$i=0;
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
    if($row)
        $returnArray[$i++] = $row;
}
mysql_free_result($result);
return $returnArray;
</code></pre>

<p>After using the code that was given to me to omit the index numbers, here is what I am now left with. Its close but not what I need.</p>

<p>Array
(
    [id] => 1
    [cust] => bobs auto
)</p>

<p>This is what the above line should read like</p>

<pre><code>'1' =&gt; 'bobs auto'
</code></pre>

<p>What I am trying to do is to format the output for a JSON call for a suggestion box.</p>

<p><br><br></p>

<p>I cannot get this to work. Here is everything after my db connection.</p>

<pre><code>$out_array = array();
foreach($items as $key=&gt;$val)
{
    if(is_int($key))
    {
      continue;
    }
    $out[$key['id']] = $val['cust'];
}



//echo'&lt;pre&gt;';
//print_r($out_array);
//echo'&lt;/pre&gt;';

foreach ($out_array as $key=&gt;$value) {
    if (strpos(strtolower($key), $q) !== false) {
     echo "$key|$value\n";
    }
}
</code></pre>

<p><hr /></p>

<p>OK, I think I am coming down to the home stretch. I have what I need sort of. This is the code I have so far.</p>

<pre><code>$out_array = array();
foreach($items as $key)
{
    $out_array[$key] = $val;
//$out_array[$key['id']] = $key['cust'];
}
</code></pre>

<p>Notice that the commented line does not work, It outputs like the id twice but the line that isn't commented out works just fine. Here is the output from that.</p>

<pre><code>Array
(
    [8] =&gt; 
    [FAT BURGER] =&gt; 
)
</code></pre>

<p>From this point, would I just use another foreach to iterate over the entire set of data? The array output you see above is from a print_r.</p>

<p><hr /></p>

<p>This is what I now have and it returns the correct association however, I must comment out the strpos condition to get any results back and I don't know why. Am I correct in nesting these foreach loops like I have?</p>

<pre><code>$out_array = array();
foreach($items as $key)
{
  //    $out_array[$key] = $val;
  $out_array[$key['id']] = $key['cust'];

  foreach ($out_array as $key=&gt;$value)
  {
      if (strpos(strtolower($key), $q) !== false)
      {
       echo "$key|$value\n";
      }
  }

}
</code></pre>
