title: Trying to reorganize this PHP array so it's values are toghether by id and easily accesible later
body: <p>First of all, I couldn't think of any better title, sorry if you find it inapropiate.</p>
  
  <p>I have this function which task is to bring data from 2 databases, modify some of that data, and upload everything to an external API. </p>
  
  <p>The first database <code>accounting</code> has </p>
  
  <p><code>subscriber_id | amount | zone_id</code> </p>
  
  <p>stored in the table named <code>cdr</code> </p>
  
  <p>The second db <code>billing</code> has stored inside the table <code>billing_zones</code> these values:</p>
  
  <p><code>zone_id | zone_name</code></p>
  
  <p>Everything I've done works fine, but the resulting array is not what I expected/wanted.</p>
  
  <p>This is my code:</p>
  
  <pre class="lang-php prettyprint-override"><code>try {
      $conn = new PDO('mysql:host=host;dbname=accounting','user','password');
      $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $conn-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  }catch(PDOException $e){
      echo "ERROR: " . $e-&gt;getMessage();
  }
  
  
  
  $destinationId = 0;
  $stmt = $conn-&gt;prepare('SELECT a.`zone`, b.`source_external_subscriber_id`, SUM(b.`source_customer_cost`) AS total
      FROM `billing`.billing_zones a INNER JOIN cdr b 
      ON a.`id` = b.`source_customer_billing_zone_id` 
      WHERE destination_account_id = :destinationId 
      GROUP BY b.`source_external_subscriber_id`, a.`zone`');
  $stmt-&gt;execute(array('destinationId' =&gt; $destinationId));
  
  foreach ($stmt as $row) {
      $data[] = $row;
  }
  
  for ($i=0; $i &lt; sizeof($data); $i++) { 
      $sanitizedData[$i] = array(
          0 =&gt; $data[$i][0],
          1 =&gt; $data[$i][1],
          2 =&gt; $data[$i][2]
      );
  }
  
  /*ATTEMPT TO MAKE THE ARRAY I WANTED - DIDN'T WORK*/
  for ($i=0; $i &lt; sizeof($data); $i++) { 
      while ($sanitizedData[$i] == $sanitizedData[$i-1][1]) {
          $newArray[] = array_merge($sanitizedData[$i], $sanitizedData[$i-1]);
      }
  }
  
  var_dump($sanitizedData);
  </code></pre>
  
  <p>The result of the code above my <code>ATTEMPT</code> comment is a big array, I will show you a piece of it so you can understand better what I want to do:</p>
  
  <pre class="lang-php prettyprint-override"><code>[1047]=&gt;
    array(3) {
      [0]=&gt;
      string(27) "Llamadas Moviles nacionales"
      [1]=&gt;
      string(9) "V30048086"
      [2]=&gt;
      string(10) "460.440000"
    }
    [1048]=&gt;
    array(3) {
      [0]=&gt;
      string(28) "Llamadas Premium 902 Nivel 1"
      [1]=&gt;
      string(9) "V30048086"
      [2]=&gt;
      string(9) "87.301236"
    }
    [1049]=&gt;
    array(3) {
      [0]=&gt;
      string(25) "Llamadas Fijos nacionales"
      [1]=&gt;
      string(9) "W24154073"
      [2]=&gt;
      string(9) "64.340367"
    }
    [1050]=&gt;
    array(3) {
      [0]=&gt;
      string(27) "Llamadas Moviles nacionales"
      [1]=&gt;
      string(9) "W24154073"
      [2]=&gt;
      string(10) "116.480000"
    }
    [1051]=&gt;
    array(3) {
      [0]=&gt;
      string(28) "Llamadas Premium 901 Nivel 1"
      [1]=&gt;
      string(9) "W24154073"
      [2]=&gt;
      string(9) "62.559759"
    }
  </code></pre>
  
  <p>To clarify:</p>
  
  <p><code>array[n][0] is the aforementioned zone_name</code></p>
  
  <p><code>array[n][1] is the subscriber_id - The id defining the customer</code></p>
  
  <p><code>array[n][2] is the SUM() of the amount for that id in that zone.</code></p>
  
  <p>So the result is that for every id, there are several arrays for each zone.</p>
  
  <p>What I want to do is to group those in a unique array like: </p>
  
  <pre class="lang-php prettyprint-override"><code>[1051]=&gt;
    array(3) {
      [0]=&gt;
      string(9) "W24154073"
      [1]=&gt;
      string(28) "Llamadas Premium 901 Nivel 1"
      [2]=&gt;
      string(9) "62.559759"
      [3]=&gt;
      string(28) "Llamadas Moviles nacionales"
      [4]=&gt;
      string(9) "116.480000"
      [5]=&gt;
      string(28) "Llamadas Fijos nacionales"
      [6]=&gt;
      string(9) "64.340367"
      .
      .
      . 
      If there were more
    }
  </code></pre>
  
  <p>As @Misorude pointed out in the comments, maybe using the subscriber_id as key for the main array would be a better solution:</p>
  
  <pre class="lang-php prettyprint-override"><code>["W24154073"]=&gt;
    array(3) {
      [0]=&gt;
      string(28) "Llamadas Premium 901 Nivel 1"
      [1]=&gt;
      string(9) "62.559759"
      [2]=&gt;
      string(28) "Llamadas Moviles nacionales"
      [3]=&gt;
      string(9) "116.480000"
      [4]=&gt;
      string(28) "Llamadas Fijos nacionales"
      [5]=&gt;
      string(9) "64.340367"
      .
      .
      . 
      If there were more
    }
  </code></pre>
  
  <p>This way, I could later access the array and fetch the info customer by customer, not having in mind how many zones they are in.</p>
  
  <p>I don't know if this is all necessary or if I don't even need any of this to begin with, but I could not think about any way to do it. Maybe I have overthinked this a lot.</p>
  
  <p>If you know how to turn the array I get into the array I want it would be great, but if there is another solution to pass the data easier than this, then please, bring me the light I need.</p>
  
  <p>Thank you for the help, have a great weekend!</p>