I am trying to combine arrays into one, for depletion I took the function array_merge , I have an array:

  Array ( [0] => Array ( [order_id] => 17 [customer_id] => 19 [firstname] => [lastname] => [email] => [telephone] => [delivery_method] => 1 [delivery_address] => [region] => 349 [country] => 18 [comment] => что то [product_name] => Часы [model_product] => ВС [quantity] => 1 [total] => 32.0000 [oll_total] => 32.0000 [language_id] => 1 [date_added] => 2017-02-09 12:04:56 [date_modified] => 2017-02-09 12:04:56 ) [1] => Array ( [name] => Сумская область ) [2] => Array ( [name] => Верхняя сыроватка ) 

I'm trying to bring the array to mind.

  Array ( [0] => Array ( [order_id] => 17 [name] => [name] => [customer_id] => 19 [firstname] => [lastname] => [email] => [telephone] => [delivery_method] => 1 [delivery_address] => [region] => 349 [country] => 18 [comment] => что то [product_name] => Часы [model_product] => ВС [quantity] => 1 [total] => 32.0000 [oll_total] => 32.0000 [language_id] => 1 [date_added] => 2017-02-09 12:04:56 [date_modified] => 2017-02-09 12:04:56 

doing an array search

 foreach ($results as $result) { $this->data['orders'][] = array( 'firstname' => $result['firstname'], 'lastname' => $result['lastname'], 'email' => $result['email'], 'telephone' => $result['telephone'], 'delivery_method' => $result['delivery_method'], ); } 

I get the error Undefined index: firstname , etc. Tell me how to do it right, if not, tell me how it is right?

redid it turned out

  Array ( [0] => Array ( [name] => [telephone] => [email] => [delivery_address] => [comment] => что то [product_name] => Часы [model_product] => ВС [quantity] => 1 [total] => 32.0000 [oll_total] => 32.0000 [date_added] => 2017-02-09 12:04:56 [name_region] => Сумская область [name_city] => Верхняя сыроватка ) 

)

$ this-> data ['orders'] = array (); $ orders = $ this-> model_account_order-> getOrder ();

  foreach ($orders as $order) { $regions = $this->model_account_order->getRegionName(); foreach ($regions as $region) { $citys = $this->model_account_order->getCityName(); foreach ($citys as $city) { $this->data['orders'][] = array( 'name' => $order['firstname'] . " " . $order['lastname'], 'telephone' => $order['telephone'], 'email' => $order['email'], 'delivery_address' => $order['delivery_address'], 'comment' => $order['comment'], 'product_name' => $order['product_name'], 'model_product' => $order['model_product'], 'quantity' => $order['quantity'], 'total' => $order['total'], 'oll_total' => $order['oll_total'], 'date_added' => $order['date_added'], 'name_region' => $region['name_region'], 'name_city' => $city['name_city'] ); } } } 
  • You can't shove 2 name into one array - vp_arth

2 answers 2

The error occurs because the first and third indexes are missing in the second and third arrays. As far as I understand arrays:

  [1] => Array ( [name] => Сумская область ) [2] => Array ( [name] => Верхняя сыроватка ) 

are semantic load, different from the first. I would in the place where they are defined simply redefine the keys, and then put these elements into the zero array. And then the code turns out you have all the same three arrays on the output, only now they are added in $ this-> data ['orders']

  • Made edits at the top tell me how to do it right? - Roman Yushko
  • Is it right or not depends on what you want to end up with. - Leonid
  • If you receive "regional" data from the form, then simply add them to the same form with the necessary keys and names. If from the database, then it is possible to set it in a select so that data with different keys is drawn out. And then everything is laid down and processed. - Leonid

Everything is very simple.
All the necessary data you already have, you only need to build an array of the required format.
This can be done in one cycle or, for example, using the array_map function.

 $this->data['orders'] = array_map(function($row){ $order = $row[0]; return array( 'name' => $order['firstname'] . " " . $order['lastname'], 'telephone' => $order['telephone'], 'email' => $order['email'], 'delivery_address' => $order['delivery_address'], 'comment' => $order['comment'], 'product_name' => $order['product_name'], 'model_product' => $order['model_product'], 'quantity' => $order['quantity'], 'total' => $order['total'], 'oll_total' => $order['oll_total'], 'date_added' => $order['date_added'], 'name_region' => $row[1]['name'], 'name_city' => $row[2]['city'], ); }, $results); 
  • I did not quite right? or I get more code - Roman Yushko
  • Actually, I wonder why you have one order refers to an array of regions and cities. - user236014
  • And in general, the code given at the end of the question has little to do with the original question. It looks like "How can I do A? Although, no, wait, I did B instead, is that right?" - user236014
  • I have been to different solutions. Sorry if something is wrong, and from regions and cities I pulled out names for withdrawal because I had an id in orderRoman Yushko