S
S
Secret732017-05-22 11:11:04
PHP
Secret73, 2017-05-22 11:11:04

How to merge identical arrays?

Hello. I can not exactly formulate my question, so I ask you for help in solving my problem. I work with the smarty templating engine.
We have an array like:

array(3) {
  ["order_num"]=>
  string(4) "1"
  ["price"]=>
  string(7) "1200.00"
  ["title"]=>
  string(61) "Майка""
}

array(3) {
  ["order_num"]=>
  string(4) "2"
  ["price"]=>
  string(6) "130.00"
  ["title"]=>
  string(63) "Футболка"
}

array(3) {
  ["order_num"]=>
  string(4) "2"
  ["price"]=>
  string(6) "350.00"
  ["title"]=>
  string(71) "Трусы"
}

Output code in the template:
<table>
{foreach $order_report_arr as $nums}
<pre>
{var_dump($nums)}
</pre>
      <tr>
        <td>Номер заказа {$nums.order_num}</td>
      </tr>
      <tr>
        <td>Название {$nums.title}</td>
        <td>Цена {$nums.price}</td>
      </tr>
{/foreach}
</table>

Output result:
Order No. 1
Name T-shirt Price 1200.00
Order No. 2
Name T-shirt Price 130.00
Order No. 2
Name Briefs Price 350.00
Question how to make it look like this:
Order No. 1
Name T-shirt Price 1200.00
Order No. 2
Name T-shirt Price 130.00
Name Briefs Price 350.00

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2017-05-23
@Secret73

group orders by number in puff, in the template just output in cycles.

S
Secret73, 2017-05-22
@Secret73

Where have all the comments gone? Here is an almost ready-made version, but how to access the desired variable here?

<table>
{foreach $order_report_arr as $nums}
      <tr>
        <td>Номер заказа {$nums.order_num}</td>
      </tr>
    {foreach $nums as $num}       
        <tr>
          <td>Название {$num}</td>
        </tr>
    {/foreach}
{/foreach}
</table>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question