Tell me it is possible to combine arrays into one, my order writes in one order table, and the goods in order_product.

order

  Array ( [0] => Array( [order_id] => 52 [firstname] => test [lastname] => tst [email] => Test [telephone] => test [payment_address_1] => Отделение №1: ул. Гагарина,22 [payment_method] => Новая почта [date_added] => 2017-03-15 15:51:44 [date_modified] => 2017-03-15 15:51:44 ) ) 

order_product

  Array ( [0] => Array ( [order_product_id] => 281 [id_order] => 52 [name] => Тапочки [model] => Закрытые [quantity] => 1 [total] => 25.00 [oll_total] => 25.00 ) [1] => Array ( [order_product_id] => 280 [id_order] => 52 [name] => Носки [model] => Закрытые [quantity] => 1 [total] => 20.50 [oll_total] => 20.50 ) 

It is possible to get an array of this kind

  Array ( [0] => Array( [order_id] => 52 [firstname] => test [lastname] => tst [email] => Test [telephone] => test [payment_address_1] => Отделение №1: ул. Гагарина,22 [payment_method] => Новая почта [name] => Тапочки,Носки [oll_total] => 25.00+20,05 [date_added] => 2017-03-15 15:51:44 [date_modified] => 2017-03-15 15:51:44 ) 

)

  • $order[0]['name] = implode(',', array_map(function($p){ return $p['name];}, $order_product)) - teran

4 answers 4

Option 1. Request to the database. MySQL example:

 SELECT o.*, GROUP_CONCAT(op.name SEPARATOR ', ') AS name, SUM(op.oll_total) AS oll_total FROM order_table o LEFT JOIN order_product_table op ON op.order_product_id = o.order_id GROUP BY o.order_id 

Option 2. Cycle in PHP, if you have 2 arrays

 $result = []; foreach ($orders as $key => $order) { $result[$key] = $order + ['name' => [], 'oll_total' => 0]; foreach ($order_products as $key2 => $product) { if ($product['order_product_id'] == $order['order_id']) { $result[$key]['name'][] = $product['name']; $result[$key]['oll_total'] += $product['oll_total']; } } $result[$key]['name'] = implode(', ', $result[$key]['name']); } 

There is no finished function. In your particular case, I would choose option 1.

  • difficult at you option on pkhp left. two separate cycles must be solved, not nested - teran
  • @teran you are right, you can optimize if $ result keys make order_id - Elena Vasilenko
  • Thanks @Elena the second way just came up - Sender1050

everything is more than obvious

 $order['products'] = $order_product 
  • Are you sure that after this action in $order['name'] will have тапочки, носки ? - teran
  • @teran did not understand the question, I simply add the array to the index of the order array, since the connection is one to many, one order and many products. And here generally `$ order ['name']? And if you need to remove the goods from the order? - Naumov
  • At the end of the question the author gives an example of the desired result. where the name and oll_total fields are added to $order based on $order_product - teran
  • @teran And this means that you can not push the author to a more correct and convenient solution, and not a castell? - Naumov
  • Duck you first answer for what is being asked, and then push on other solutions. Without knowing the specific nature of the problem being solved, it is impossible to say with confidence that this is a crutch. - teran

merge() can be used to combine arrays
Something like this:

 var arr1=[1,2,3];<br> var arr2=["a","b","c"];<br> $.merge(arr1, arr2); 
  • It is better to use array_merge_recursive() , since the arrays have the same keys - Vasily Koshelev

Just add arrays:

  $arr1 = array( [ "order_id" => 52, "firstname" => "test", "lastname" => "tst", "email" => "Test", "telephone" => "test", "payment_address_1" => "Отделение №1: ул. Гагарина,22", "payment_method" => "Новая почта", "date_added" => "2017-03-15 15:51:44", "date_modified" => "2017-03-15 15:51:44 " ] ) ; $arr2 = array( [ "order_product_id" => 52, "id_order" => 52, "name" => "Носки", "model" => "Закрытые", "quantity" => 1, "total" => 25.00, "oll_total" => 25.00, ], [ "order_product_id" => 280, "id_order" => 52, "name" => "Тапочки", "model" => "Закрытые", "quantity" => 1, "total" => 25.00, "oll_total" => 25.00, ], ) ; $result = $arr1 + $arr2; 

Result:

http://prntscr.com/ekltaa

  • you read in general what the author is asking, at the end is the view of the desired array, what's the addition? - teran
  • @teran and really, I'm sorry - Kostiantyn Okhotnyk