title: how to assemble data in an array by a value in php
body: <p>i have an array like this :</p>
  <pre><code>[
      ['external_account' =&gt; 'AAA', 'label' =&gt; 'Nbr de client', 'value' =&gt; 1],
      ['external_account' =&gt; 'AAA', 'label' =&gt; 'TTC', 'value' =&gt; 1],
      ['external_account' =&gt; 'BBB', 'label' =&gt; 'HT', 'value' =&gt; 0],
  ]
  </code></pre>
  <p>and i want to assemble data by external_account value and have an outpout (array) like this one</p>
  <pre><code>[
      [
          'external_account' =&gt; 'AAA',
          'data' =&gt; [
              ['label' =&gt; 'Nbr de client', 'value' =&gt; 1],
              ['label' =&gt; 'TTC', 'value' =&gt; 1]
          ]
      ],
      [
          'external_account' =&gt; 'BBB',
          'data' =&gt; [
              ['label' =&gt; 'HT', 'value' =&gt; 0]
          ]
      ]
  ]
  </code></pre>
  <p>my code so far :</p>
  <pre><code>foreach ($result as $current_key =&gt; $current_array) {
      $assemble = [];
      foreach ($result as $search_key =&gt; $search_array) {
          if (
              $current_key != $search_key &amp;&amp;
              $current_array[&quot;external_account&quot;] ==
                  $search_array[&quot;external_account&quot;]
          ) {
              $valeur = $current_array[&quot;external_account&quot;];
  
              array_push(
                  $assemble,
                  $current_array[&quot;label&quot;],
                  $current_array[&quot;value&quot;]
              );
              unset($result[$current_key]);
          }
          $data[] = [&quot;external_account&quot; =&gt; $valeur, &quot;data&quot; =&gt; $assemble];
      }
  }
  </code></pre>
  <p>please tell me what am doing wrong, am stuck on it.</p>
  <p>The output of my code : <a href="https://pastebin.com/2NBEzaq7" rel="nofollow noreferrer">enter link description here</a></p>