У меня есть следующий массив, который имеет повторяющиеся данные:
Array ( [0] => Array ( [orders] => Array ( [id] => 9 [name] => Abdus Sattar Bhuiyan [email] => sattar.kuet@gmail.com [mobile] => 01673050495 [alt_mobile] => 01818953250 [city_id] => 2 [location_id] => 5 [status] => No contact [chashed] => NO [created] => 2015-06-27 12:49:34 [modified] => 2015-06-27 12:49:34 [comment] => ) [order_products] => Array ( [id] => 2 [order_id] => 9 [product_id] => 1 [pieces] => 1 ) [products] => Array ( [id] => 1 [category_id] => 1 [name] => নভোযানের নাম সি প্রোগ্রামিং [writer] => Engr. Abdus Sattar Bhuiyan [created] => 2015-06-24 16:17:45 ) [psettings] => Array ( [id] => 1 [category_id] => 1 [product_id] => 1 [img] => 1.jpg [desc] => description [created] => 2015-06-28 00:28:26 [bppp] => 44000 [sppp] => 45000 [discount] => 25 [service_charge] => 30 ) ) [1] => Array ( [orders] => Array ( [id] => 10 [name] => Abdus Sattar Bhuiyan [email] => sattar.kuet@gmail.com [mobile] => 01673050495 [alt_mobile] => 01818953250 [city_id] => 1 [location_id] => 1 [status] => No contact [chashed] => NO [created] => 2015-06-28 03:30:25 [modified] => 2015-06-28 03:30:25 [comment] => ) [order_products] => Array ( [id] => 6 [order_id] => 10 [product_id] => 1 [pieces] => 1 ) [products] => Array ( [id] => 1 [category_id] => 1 [name] => নভোযানের নাম সি প্রোগ্রামিং [writer] => Engr. Abdus Sattar Bhuiyan [created] => 2015-06-24 16:17:45 ) [psettings] => Array ( [id] => 1 [category_id] => 1 [product_id] => 1 [img] => 1.jpg [desc] => description [created] => 2015-06-28 00:28:26 [bppp] => 44000 [sppp] => 45000 [discount] => 25 [service_charge] => 30 ) ) [2] => Array ( [orders] => Array ( [id] => 9 [name] => Abdus Sattar Bhuiyan [email] => sattar.kuet@gmail.com [mobile] => 01673050495 [alt_mobile] => 01818953250 [city_id] => 2 [location_id] => 5 [status] => No contact [chashed] => NO [created] => 2015-06-27 12:49:34 [modified] => 2015-06-27 12:49:34 [comment] => ) [order_products] => Array ( [id] => 3 [order_id] => 9 [product_id] => 2 [pieces] => 1 ) [products] => Array ( [id] => 2 [category_id] => 1 [name] => Resonance of creativity with C++ [writer] => Engr. Abdus Sattar Bhuiyan [created] => 2015-06-26 07:32:52 ) [psettings] => Array ( [id] => 2 [category_id] => 1 [product_id] => 2 [img] => 2.jpg [desc] => [created] => 2015-06-26 07:33:41 [bppp] => 150 [sppp] => 250 [discount] => 20 [service_charge] => 30 ) ) [3] => Array ( [orders] => Array ( [id] => 10 [name] => Abdus Sattar Bhuiyan [email] => sattar.kuet@gmail.com [mobile] => 01673050495 [alt_mobile] => 01818953250 [city_id] => 1 [location_id] => 1 [status] => No contact [chashed] => NO [created] => 2015-06-28 03:30:25 [modified] => 2015-06-28 03:30:25 [comment] => ) [order_products] => Array ( [id] => 5 [order_id] => 10 [product_id] => 2 [pieces] => 1 ) [products] => Array ( [id] => 2 [category_id] => 1 [name] => Resonance of creativity with C++ [writer] => Engr. Abdus Sattar Bhuiyan [created] => 2015-06-26 07:32:52 ) [psettings] => Array ( [id] => 2 [category_id] => 1 [product_id] => 2 [img] => 2.jpg [desc] => [created] => 2015-06-26 07:33:41 [bppp] => 150 [sppp] => 250 [discount] => 20 [service_charge] => 30 ) ) [4] => Array ( [orders] => Array ( [id] => 9 [name] => Abdus Sattar Bhuiyan [email] => sattar.kuet@gmail.com [mobile] => 01673050495 [alt_mobile] => 01818953250 [city_id] => 2 [location_id] => 5 [status] => No contact [chashed] => NO [created] => 2015-06-27 12:49:34 [modified] => 2015-06-27 12:49:34 [comment] => ) [order_products] => Array ( [id] => 4 [order_id] => 9 [product_id] => 3 [pieces] => 1 ) [products] => Array ( [id] => 3 [category_id] => 1 [name] => programming by story C [writer] => Hasibul Hasan Shanto [created] => 2015-06-26 07:35:57 ) [psettings] => Array ( [id] => 3 [category_id] => 1 [product_id] => 3 [img] => 3.jpg [desc] => [created] => 2015-06-26 07:36:26 [bppp] => 150 [sppp] => 250 [discount] => 10 [service_charge] => 30 ) ) [5] => Array ( [orders] => Array ( [id] => 10 [name] => Abdus Sattar Bhuiyan [email] => sattar.kuet@gmail.com [mobile] => 01673050495 [alt_mobile] => 01818953250 [city_id] => 1 [location_id] => 1 [status] => No contact [chashed] => NO [created] => 2015-06-28 03:30:25 [modified] => 2015-06-28 03:30:25 [comment] => ) [order_products] => Array ( [id] => 7 [order_id] => 10 [product_id] => 3 [pieces] => 1 ) [products] => Array ( [id] => 3 [category_id] => 1 [name] => programming by story C [writer] => Hasibul Hasan Shanto [created] => 2015-06-26 07:35:57 ) [psettings] => Array ( [id] => 3 [category_id] => 1 [product_id] => 3 [img] => 3.jpg [desc] => [created] => 2015-06-26 07:36:26 [bppp] => 150 [sppp] => 250 [discount] => 10 [service_charge] => 30 ) ) )
Я хочу отформатировать этот массив и создать следующий массив:
Array ( [0] => Array ( [orders] => Array ( [id] => 9 [name] => Abdus Sattar Bhuiyan [email] => sattar.kuet@gmail.com [mobile] => 01673050495 [alt_mobile] => 01818953250 [city_id] => 2 [location_id] => 5 [status] => No contact [chashed] => NO [created] => 2015-06-27 12:49:34 [modified] => 2015-06-27 12:49:34 [comment] => ) [order_products] => Array ( [0] => Array ( [id] => 2 [order_id] => 9 [product_id] => 1 [pieces] => 1 ) [1] => Array ( [id] => 3 [order_id] => 9 [product_id] => 2 [pieces] => 1 ) [2] => Array ( [id] => 4 [order_id] => 9 [product_id] => 3 [pieces] => 1 ) ) [products] => Array ( [0] => Array ( [id] => 1 [category_id] => 1 [name] => নভোযানের নাম সি প্রোগ্রামিং [writer] => Engr. Abdus Sattar Bhuiyan [created] => 2015-06-24 16:17:45 ) [1] => Array ( [id] => 2 [category_id] => 1 [name] => Resonance of creativity with C++ [writer] => Engr. Abdus Sattar Bhuiyan [created] => 2015-06-26 07:32:52 ) [2] => Array ( [id] => 3 [category_id] => 1 [name] => programming by story C [writer] => Hasibul Hasan Shanto [created] => 2015-06-26 07:35:57 ) ) [psettings] => Array ( [0] => Array ( [id] => 1 [category_id] => 1 [product_id] => 1 [img] => 1.jpg [desc] => description [created] => 2015-06-28 00:28:26 [bppp] => 44000 [sppp] => 45000 [discount] => 25 [service_charge] => 30 ) [1] => Array ( [id] => 2 [category_id] => 1 [product_id] => 2 [img] => 2.jpg [desc] => [created] => 2015-06-26 07:33:41 [bppp] => 150 [sppp] => 250 [discount] => 20 [service_charge] => 30 ) [2] => Array ( [id] => 3 [category_id] => 1 [product_id] => 3 [img] => 3.jpg [desc] => [created] => 2015-06-26 07:36:26 [bppp] => 150 [sppp] => 250 [discount] => 10 [service_charge] => 30 ) ) ) [1] => Array ( [orders] => Array ( [id] => 10 [name] => Abdus Sattar Bhuiyan [email] => sattar.kuet@gmail.com [mobile] => 01673050495 [alt_mobile] => 01818953250 [city_id] => 1 [location_id] => 1 [status] => No contact [chashed] => NO [created] => 2015-06-28 03:30:25 [modified] => 2015-06-28 03:30:25 [comment] => ) [order_products] => Array ( [0] => Array ( [id] => 6 [order_id] => 10 [product_id] => 1 [pieces] => 1 ) [1] => Array ( [id] => 5 [order_id] => 10 [product_id] => 2 [pieces] => 1 ) [2] => Array ( [id] => 7 [order_id] => 10 [product_id] => 3 [pieces] => 1 ) ) [products] => Array ( [0] => Array ( [id] => 1 [category_id] => 1 [name] => নভোযানের নাম সি প্রোগ্রামিং [writer] => Engr. Abdus Sattar Bhuiyan [created] => 2015-06-24 16:17:45 ) [1] => Array ( [id] => 2 [category_id] => 1 [name] => Resonance of creativity with C++ [writer] => Engr. Abdus Sattar Bhuiyan [created] => 2015-06-26 07:32:52 ) [2] => Array ( [id] => 3 [category_id] => 1 [name] => programming by story C [writer] => Hasibul Hasan Shanto [created] => 2015-06-26 07:35:57 ) ) [psettings] => Array ( [0] => Array ( [id] => 1 [category_id] => 1 [product_id] => 1 [img] => 1.jpg [desc] => description [created] => 2015-06-28 00:28:26 [bppp] => 44000 [sppp] => 45000 [discount] => 25 [service_charge] => 30 ) [1] => Array ( [id] => 2 [category_id] => 1 [product_id] => 2 [img] => 2.jpg [desc] => [created] => 2015-06-26 07:33:41 [bppp] => 150 [sppp] => 250 [discount] => 20 [service_charge] => 30 ) [2] => Array ( [id] => 3 [category_id] => 1 [product_id] => 3 [img] => 3.jpg [desc] => [created] => 2015-06-26 07:36:26 [bppp] => 150 [sppp] => 250 [discount] => 10 [service_charge] => 30 ) ) ) )
Как я могу это сделать. Я переформатирую простой массив, но это не имеет смысла для форматирования. Это заставляет меня плакать. Пожалуйста, помогите мне. Если будет предложена какая-либо вспомогательная функция, это будет действительно подарок. Заранее спасибо.
Вам нужно сделать, как показано ниже: –
<?php $firstArray = Array ( '0' => Array ( 'orders' => Array ( 'id' => 9, 'name' => 'Abdus Sattar Bhuiyan', 'email' => 'sattar.kuet@gmail.com', 'mobile' => '01673050495', 'alt_mobile' => '01818953250', 'city_id' => 2, 'location_id' => 5, 'status' => 'No contact', 'chashed' => 'NO', 'created' => '2015-06-27 12:49:34', 'modified' => '2015-06-27 12:49:34', 'comment' => '' ), 'order_products' => Array ( 'id' => 2, 'order_id' => 9, 'product_id' => 1, 'pieces' => 1 ), 'products' => Array ( 'id' => 1, 'category_id' => 1, 'name' => 'নভোযানের নাম সি প্রোগ্রামিং', 'writer' => 'Engr. Abdus Sattar Bhuiyan', 'created' => '2015-06-24 16:17:45' ), 'psettings' => Array ( 'id' => 1, 'category_id' => 1, 'product_id' => 1, 'img' => '1.jpg', 'desc' => 'description', 'created' => '2015-06-28 00:28:26', 'bppp' => 44000, 'sppp' => 45000, 'discount' => 25, 'service_charge' => 30 ), ), '1' => Array ( 'orders' => Array ( 'id' => 9, 'name' => 'Abdus Sattar Bhuiyan', 'email' => 'sattar.kuet@gmail.com', 'mobile' => '01673050495', 'alt_mobile' => '01818953250', 'city_id' => 2, 'location_id' => 5, 'status' => 'No contact', 'chashed' => 'NO', 'created' => '2015-06-27 12:49:34', 'modified' => '2015-06-27 12:49:34', 'comment' => '' ), 'order_products' => Array ( 'id' => 3, 'order_id' => 9, 'product_id' => 2, 'pieces' => 1 ), 'products' => Array ( 'id' => 2, 'category_id' => 1, 'name' => 'Resonance of creativity with C++', 'writer' => 'Engr. Abdus Sattar Bhuiyan', 'created' => '2015-06-26 07:32:52' ), 'psettings' => Array ( 'id' => 2, 'category_id' => 1, 'product_id' => 2, 'img' => '2.jpg', 'desc' => '', 'created' => '2015-06-26 07:33:41', 'bppp' => 150, 'sppp' => 250, 'discount' => 20, 'service_charge' => 30 ), ), '2' => Array ( 'orders' => Array ( 'id' => 9, 'name' => 'Abdus Sattar Bhuiyan', 'email' => 'sattar.kuet@gmail.com', 'mobile' => '01673050495', 'alt_mobile' => '01818953250', 'city_id' => 2, 'location_id' => 5, 'status' => 'No contact', 'chashed' => 'NO', 'created' => '2015-06-27 12:49:34', 'modified' => '2015-06-27 12:49:34', 'comment' => '' ), 'order_products' => Array ( 'id' => 4, 'order_id' => 9, 'product_id' => 3, 'pieces' => 1 ), 'products' => Array ( 'id' => 3, 'category_id' => 1, 'name' => 'programming by story C', 'writer' => 'Hasibul Hasan Shanto', 'created' => '2015-06-26 07:35:57' ), 'psettings' => Array ( 'id' => 3, 'category_id' => 1, 'product_id' => 3, 'img' => '3.jpg', 'desc' => '', 'created' => '2015-06-26 07:36:26', 'bppp' => 150, 'sppp' => 250, 'discount' => 10, 'service_charge' => 30 ), ), ); $new_array = array(); foreach($firstArray as $key=> $arr){ if($key == 0){ $new_array[0]['orders'] = $arr['orders']; $new_array[0]['order_products'][] = $arr['order_products']; $new_array[0]['products'][] = $arr['products']; $new_array[0]['psettings'][] = $arr['psettings']; }else{ foreach($new_array as $key=> $newarr){ if($arr['orders'] == $newarr['orders']){ $new_array[$key]['order_products'][] = $arr['order_products']; $new_array[$key]['products'][] = $arr['products']; $new_array[$key]['psettings'][] = $arr['psettings']; }else{ $new_array[] = $arr; } } } } echo "<pre/>";print_r($new_array); ?>
Выход: – https://eval.in/388565 .
Примечание: – не беспокойтесь о массиве, который я положил. Я просто беру ваш массив и отформатировал его для запуска цели и проверки на моем конце. Благодарю.
Это довольно простая задача для array_column()
( ручная ссылка ). Подобно этому ( $array
– ваш источник):
$composite = []; $composite['orders'] = array_column($array, 'orders')[0]; $composite['order_products'] = array_column($array, 'order_products'); $composite['products'] = array_column($array, 'products'); $composite['psettings'] = array_column($array, 'psettings'); print_r($composite);
Это приводит к следующему массиву:
Array ( [orders] => Array ( [id] => 9 [name] => Abdus Sattar Bhuiyan [email] => sattar.kuet@gmail.com [mobile] => 01673050495 [alt_mobile] => 01818953250 [city_id] => 2 [location_id] => 5 [status] => No contact [chashed] => NO [created] => 2015-06-27 12:49:34 [modified] => 2015-06-27 12:49:34 [comment] => ) [order_products] => Array ( [0] => Array ( [id] => 2 [order_id] => 9 [product_id] => 1 [pieces] => 1 ) [1] => Array ( [id] => 3 [order_id] => 9 [product_id] => 2 [pieces] => 1 ) [2] => Array ( [id] => 4 [order_id] => 9 [product_id] => 3 [pieces] => 1 ) ) [products] => Array ( [0] => Array ( [id] => 1 [category_id] => 1 [name] => নভোযানের নাম সি প্রোগ্রামিং [writer] => Engr. Abdus Sattar Bhuiyan [created] => 2015-06-24 16:17:45 ) [1] => Array ( [id] => 2 [category_id] => 1 [name] => Resonance of creativity with C++ [writer] => Engr. Abdus Sattar Bhuiyan [created] => 2015-06-26 07:32:52 ) [2] => Array ( [id] => 3 [category_id] => 1 [name] => programming by story C [writer] => Hasibul Hasan Shanto [created] => 2015-06-26 07:35:57 ) ) [psettings] => Array ( [0] => Array ( [id] => 1 [category_id] => 1 [product_id] => 1 [img] => 1.jpg [desc] => description [created] => 2015-06-28 00:28:26 [bppp] => 44000 [sppp] => 45000 [discount] => 25 [service_charge] => 30 ) [1] => Array ( [id] => 2 [category_id] => 1 [product_id] => 2 [img] => 2.jpg [desc] => [created] => 2015-06-26 07:33:41 [bppp] => 150 [sppp] => 250 [discount] => 20 [service_charge] => 30 ) [2] => Array ( [id] => 3 [category_id] => 1 [product_id] => 3 [img] => 3.jpg [desc] => [created] => 2015-06-26 07:36:26 [bppp] => 150 [sppp] => 250 [discount] => 10 [service_charge] => 30 ) ) )
Если вам нужно собрать несколько таких массивов в один главный массив, который содержит все это, вы можете, например, обернуть его в простой цикл и добавить числовой индекс для каждого составного набора:
$composite = []; foreach($arrays as $num=>$array) { $composite[$num]['orders'] = array_column... // etc. }
Также обратите внимание на функцию index_key
для array_column
упомянутую в руководстве, может пригодиться, если вы хотите использовать идентификаторы заказов в качестве основных ключей — хотя для этого вам придется немного перефразировать логику цикла , Дайте мне знать, если это актуальная проблема, и я добавлю больше; или если этого достаточно как есть.