R
R
Ruslan Tilyaev2020-04-17 12:17:15
PHP
Ruslan Tilyaev, 2020-04-17 12:17:15

How to write data to an array?

I need to display one order, one order can contain any number of products. I have one order displayed - one product, that is, one product in one order. And this order is duplicated in such quantity as the goods in this order.

foreach ($order as $item){
                        $arr = array(
                                ' Новый заказ❗' => '',
                                ' Товары: ' => $item['name'],
                                ' Дата: ' => date('d.m.Y в H:i'),
                                ' Управление заказами' => '<a href="https://stroymaz-beta.uz/admin/orders/new">Перейти</a>',
                            );

                      debug($arr);
}

Accordingly, the result is:
Array
(
    [ Новый заказ❗] => 
    [ Товары: ] => Товар
    [ Дата: ] => 17.04.2020 в 12:09
    [ Управление заказами] => Перейти
)
Array
(
    [ Новый заказ❗] => 
    [ Товары: ] => Товар2
    [ Дата: ] => 17.04.2020 в 12:09
    [ Управление заказами] => Перейти
)
Array
(
    [ Новый заказ❗] => 
    [ Товары: ] => Товар3
    [ Дата: ] => 17.04.2020 в 12:09
    [ Управление заказами] => Перейти
)


I need the result to be like this:
Array
(
    [ Новый заказ❗] => 
    [ Товары: ] => Array
        (
            [0] => Товар
            [1] => Товар2
            [2] => Товар3
        )

    [ Дата: ] => 17.04.2020 в 12:15
    [ Управление заказами] => Перейти
)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Abr_ya, 2020-04-17
@Heckfy325

If I understood the question correctly, then in general terms I would do this:
1) in foreach, leave only the formation of an array of goods,
2) after the loop, create the desired array, substituting the one obtained in the first step into the goods (if I understood everything correctly, except for the goods, in item-ax is repeated).

0
0xD34F, 2020-04-17
@0xD34F

Remove foreach, put in "goods" instead
$item['name']
array_column($order, 'name')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question