There is an array:

Array ( [0] => Array ( [0] => Array ( [order_id] => 4 [entry_id] => 5 [count] => 1 [price] => 1 [weight] => 0 ) [1] => Array ( [order_id] => 4 [entry_id] => 2 [count] => 1 [price] => 94.4 [weight] => 0.5 ) [2] => Array ( [order_id] => 4 [entry_id] => 1 [count] => 1 [price] => 38 [weight] => 2 ) ) [1] => Array ( [0] => Array ( [order_id] => 3 [entry_id] => 4 [count] => 1 [price] => 0 [weight] => 0 ) [1] => Array ( [order_id] => 3 [entry_id] => 5 [count] => 1 [price] => 1 [weight] => 0 ) [2] => Array ( [order_id] => 3 [entry_id] => 2 [count] => 1 [price] => 94.4 [weight] => 0.5 ) [3] => Array ( [order_id] => 3 [entry_id] => 1 [count] => 1 [price] => 38 [weight] => 2 ) ) [2] => Array ( [0] => Array ( [order_id] => 2 [entry_id] => 2 [count] => 1 [price] => 94.4 [weight] => 0.5 ) [1] => Array ( [order_id] => 2 [entry_id] => 1 [count] => 1 [price] => 38 [weight] => 2 ) ) [3] => Array ( [0] => Array ( [order_id] => 1 [entry_id] => 2 [count] => 1 [price] => 94.4 [weight] => 0.5 ) ) ) 

And the script:

 $template_orders_prepare = array(); for($j = 0; $j < count($orders_prepare_list); $j++) { for($h = 0; $h < count($orders_prepare_list[$j]); $h++) { $order_id = $orders_prepare_list[$j][$h]['order_id']; $entry_id = $orders_prepare_list[$j][$h]['entry_id']; $template_orders_prepare[$order_id] = $entry_id; } } print_r($template_orders_prepare); 

The problem is specifically in this line, because entry_id given last, although there are several:

 $template_orders_prepare[$order_id] = $entry_id; 

And to give them a few, I did this:

 $template_orders_prepare[$order_id] .= $entry_id; 

And it worked as it should ( print_r($template_orders_prepare); ).

It was:

 Array ( [4] => 1 [3] => 1 [2] => 1 [1] => 2 ) 

It became:

 ( [4] => 521 [3] => 4521 [2] => 21 [1] => 2 ) 

But not everything is so simple. It would seem, it outputs everything as needed, but it is not clear where, the following errors come out:

 Notice: Undefined offset: 4 in ... Notice: Undefined offset: 3 in ... Notice: Undefined offset: 2 in ... Notice: Undefined offset: 1 in ... 

Which point to this unfortunate line:

 $template_orders_prepare[$order_id] .= $entry_id; 

What only I did not do, since 5 probably rewrote everything and to no avail. After tormenting for 2 hours, I still did not understand why this happens, but at the same time the array also contains what is needed ..

  • because the $template_orders_prepare[$order_id] .= $entry_id; says to concatenate with the contents of an element of an array that does not exist. - etki
  • @Etki, that's it .. Can I rewrite this construct somehow? - ModaL
  • $template_orders_prepare[$order_id] = $entry_id; - etki
  • @Etki, in this case, only the last $ entry_id is displayed, and there are several. In a post wrote about it. - ModaL
  • Sorry, but I can not read in detail two or three monitors. Check the presence of the element, if not, create, if there is, then supplement. Why do you need to churn all this into a line — I’m not going to attach my mind. - etki

2 answers 2

do this:

 $template_orders_prepare[$order_id][] = $entry_id; 

and after the nested loop, you can combine via implode:

$template_orders_prepare[$order_id] = implode('', $template_orders_prepare[$order_id]) ;

  • It's fine! Thank you so much, helped out more than ever! - ModaL
  • 2
    someone's like never? :)) good luck! - splash58
 $template_orders_prepare = array(); for($j = 0; $j < count($orders_prepare_list); $j++) { for($h = 0; $h < count($orders_prepare_list[$j]); $h++) { $order_id = $orders_prepare_list[$j][$h]['order_id']; $entry_id = $orders_prepare_list[$j][$h]['entry_id']; // если элемент не существует, то надо его подготовить для использования, чтобы PHP не исправлял эту ситуацию сам - для него это выглядит, как ошибка if (!isset($template_orders_prepare[$order_id])) { $template_orders_prepare[$order_id] = array(); } $template_orders_prepare[$order_id][] = $entry_id; } } print_r($template_orders_prepare);