title: PHP - Push data to array in foreach loop
body: <p>I want to achieve below array format: </p>
  
  <pre><code>{
      "success": true,
      "results": [
        {
          "name"  : "Choice 1",
          "value" : "value1",
          "text"  : "Choice 1"
        },
        {
          "name"  : "Choice 2",
          "value" : "value2",
          "text"  : "Choice 2"
        }
      ]
  }
  </code></pre>
  
  <p>However, I am using PHP and a foreach loop, to return some values from my database:</p>
  
  <pre><code>//Search for clients in our database.
  $stmt = $dbh-&gt;prepare("SELECT * FROM customers");
  $stmt-&gt;execute();
  $showAll = $stmt-&gt;fetchAll();
  </code></pre>
  
  <p>I then have my first part of the array, and my foreach loop:</p>
  
  <pre><code>$data = array(
      "success" =&gt; false,
      "results" =&gt; array()
  );
  
  foreach ($showAll as $client) {  
                 $data_array[] = 
                       array(
                       'name' =&gt; $client['name'],
                       'value' =&gt; $client['name'],
                       'text' =&gt; $client['name']
                  );
  
  }
  </code></pre>
  
  <p>Above only outputs:</p>
  
  <pre><code>[
   {
     "name":"Choice 1",
     "value":"value 1",
     "text":"Choice 1"
   },
   {
     "name":"Choice 2",
     "value":"value2",
     "text":"Choice 2"
   }
  ]
  </code></pre>
  
  <p>So it is missing the top part of my original array - but I want to loop through each database results in <code>"results": [ ... ]</code></p>