I have arrays:

'response' => array ( 0 => array ( 'barcode' => '', 'category_name' => 'Кофе', 'unit' => 'kg', 'cost' => '791', 'fiscal' => '0', 'hidden' => '0', 'menu_category_id' => '1', 'workshop' => '1', 'nodiscount' => '0', 'photo' => '/upload/pos_cdb_7631/menu/product_1464608672_3.jpg', 'photo_origin' => '/upload/pos_cdb_7631/menu/product_1464608672_3_original.jpg', 'price' => array ( 1 => '3000', 2 => '3000', ), 'product_code' => '', 'product_id' => '3', 'product_name' => 'Капучино 250 мл', 'profit' => array ( 1 => '2209', 2 => '2209', ), 'sort_order' => '999', 'tax_id' => '0', 'product_tax_id' => '0', 'type' => '2', 'weight_flag' => '0', 'color' => 'white', 'spots' => array ( 0 => array ( 'spot_id' => '1', 'price' => '3000', 'profit' => '2209', 'visible' => '0', ), 1 => array ( 'spot_id' => '2', 'price' => '3000', 'profit' => '2209', 'visible' => '0', ), ), 'ingredient_id' => '0', 'out' => 248, 'product_production_description' => '', 'ingredients' => array ( 0 => array ( 'structure_id' => '52', 'ingredient_id' => '10', 'pr_in_clear' => '0', 'pr_in_cook' => '0', 'pr_in_fry' => '0', 'pr_in_stew' => '0', 'pr_in_bake' => '0', 'structure_unit' => 'l', 'structure_type' => '1', 'structure_brutto' => 30, 'structure_netto' => 30, 'structure_lock' => '1', 'structure_selfprice' => '2', 'ingredient_name' => 'Вода', 'ingredient_unit' => 'l', 'ingredient_weight' => 0, 'ingredients_losses_clear' => '0', 'ingredients_losses_cook' => '0', 'ingredients_losses_fry' => '0', 'ingredients_losses_stew' => '0', 'ingredients_losses_bake' => '0', ), 1 => array ( 'structure_id' => '53', 'ingredient_id' => '9', 'pr_in_clear' => '0', 'pr_in_cook' => '0', 'pr_in_fry' => '0', 'pr_in_stew' => '0', 'pr_in_bake' => '0', 'structure_unit' => 'kg', 'structure_type' => '1', 'structure_brutto' => 8, 'structure_netto' => 8, 'structure_lock' => '1', 'structure_selfprice' => '232', 'ingredient_name' => 'Кофе', 'ingredient_unit' => 'kg', 'ingredient_weight' => 0, 'ingredients_losses_clear' => '0', 'ingredients_losses_cook' => '0', 'ingredients_losses_fry' => '0', 'ingredients_losses_stew' => '0', 'ingredients_losses_bake' => '0', ), 2 => array ( 'structure_id' => '54', 'ingredient_id' => '8', 'pr_in_clear' => '0', 'pr_in_cook' => '0', 'pr_in_fry' => '0', 'pr_in_stew' => '0', 'pr_in_bake' => '0', 'structure_unit' => 'l', 'structure_type' => '1', 'structure_brutto' => 150, 'structure_netto' => 210, 'structure_lock' => '0', 'structure_selfprice' => '330', 'ingredient_name' => 'Молоко', 'ingredient_unit' => 'l', 'ingredient_weight' => 0, 'ingredients_losses_clear' => '0', 'ingredients_losses_cook' => '0', 'ingredients_losses_fry' => '0', 'ingredients_losses_stew' => '0', 'ingredients_losses_bake' => '0', ), 3 => array ( 'structure_id' => '55', 'ingredient_id' => '7', 'pr_in_clear' => '0', 'pr_in_cook' => '0', 'pr_in_fry' => '0', 'pr_in_stew' => '0', 'pr_in_bake' => '0', 'structure_unit' => 'p', 'structure_type' => '1', 'structure_brutto' => 1, 'structure_netto' => 0, 'structure_lock' => '1', 'structure_selfprice' => '190', 'ingredient_name' => 'Бумажные стаканы 250 мл', 'ingredient_unit' => 'p', 'ingredient_weight' => 0, 'ingredients_losses_clear' => '0', 'ingredients_losses_cook' => '0', 'ingredients_losses_fry' => '0', 'ingredients_losses_stew' => '0', 'ingredients_losses_bake' => '0', ), 4 => array ( 'structure_id' => '56', 'ingredient_id' => '6', 'pr_in_clear' => '0', 'pr_in_cook' => '0', 'pr_in_fry' => '0', 'pr_in_stew' => '0', 'pr_in_bake' => '0', 'structure_unit' => 'p', 'structure_type' => '1', 'structure_brutto' => 1, 'structure_netto' => 0, 'structure_lock' => '1', 'structure_selfprice' => '15', 'ingredient_name' => 'Крышка 250 мл', 'ingredient_unit' => 'p', 'ingredient_weight' => 0, 'ingredients_losses_clear' => '0', 'ingredients_losses_cook' => '0', 'ingredients_losses_fry' => '0', 'ingredients_losses_stew' => '0', 'ingredients_losses_bake' => '0', ), 5 => array ( 'structure_id' => '57', 'ingredient_id' => '5', 'pr_in_clear' => '0', 'pr_in_cook' => '0', 'pr_in_fry' => '0', 'pr_in_stew' => '0', 'pr_in_bake' => '0', 'structure_unit' => 'p', 'structure_type' => '1', 'structure_brutto' => 1, 'structure_netto' => 0, 'structure_lock' => '1', 'structure_selfprice' => '22', 'ingredient_name' => 'Размешиватель', 'ingredient_unit' => 'p', 'ingredient_weight' => 0, 'ingredients_losses_clear' => '0', 'ingredients_losses_cook' => '0', 'ingredients_losses_fry' => '0', 'ingredients_losses_stew' => '0', 'ingredients_losses_bake' => '0', ), ), ), 1 => array ( 'barcode' => '', 'category_name' => 'Кофе', 'unit' => 'kg', 'cost' => '805', 'fiscal' => '0', 'hidden' => '0', 'menu_category_id' => '1', 'workshop' => '2', 'nodiscount' => '0', 'photo' => '/upload/pos_cdb_7631/menu/product_1464251849_5.jpg', 'photo_origin' => '/upload/pos_cdb_7631/menu/product_1464251849_5_original.jpg', 'price' => array ( 1 => '1600', 2 => '1600', ), 'product_code' => '', 'product_id' => '5', 'product_name' => 'Круассан с шоколадом', 'profit' => array ( 1 => '795', 2 => '795', ), 'sort_order' => '999', 'tax_id' => '0', 'product_tax_id' => '0', 'type' => '2', 'weight_flag' => '0', 'color' => 'white', 'spots' => array ( 0 => array ( 'spot_id' => '1', 'price' => '1600', 'profit' => '795', 'visible' => '0', ), 1 => array ( 'spot_id' => '2', 'price' => '1600', 'profit' => '795', 'visible' => '0', ), ), 'ingredient_id' => '0', 'out' => 190, 'product_production_description' => '', 'ingredients' => array ( 0 => array ( 'structure_id' => '62', 'ingredient_id' => '17', 'pr_in_clear' => '0', 'pr_in_cook' => '0', 'pr_in_fry' => '0', 'pr_in_stew' => '0', 'pr_in_bake' => '0', 'structure_unit' => 'kg', 'structure_type' => '1', 'structure_brutto' => 30, 'structure_netto' => 30, 'structure_lock' => '1', 'structure_selfprice' => '360', 'ingredient_name' => 'Шоколад черный', 'ingredient_unit' => 'kg', 'ingredient_weight' => 0, 'ingredients_losses_clear' => '0', 'ingredients_losses_cook' => '0', 'ingredients_losses_fry' => '0', 'ingredients_losses_stew' => '0', 'ingredients_losses_bake' => '0', ), 1 => array ( 'structure_id' => '63', 'ingredient_id' => '4', 'pr_in_clear' => '0', 'pr_in_cook' => '0', 'pr_in_fry' => '0', 'pr_in_stew' => '0', 'pr_in_bake' => '0', 'structure_unit' => 'kg', 'structure_type' => '2', 'structure_brutto' => 130, 'structure_netto' => 160, 'structure_lock' => '0', 'structure_selfprice' => '445', 'ingredient_name' => 'Тесто для круассанов', 'ingredient_unit' => 'kg', 'ingredient_weight' => 0, 'ingredients_losses_clear' => '0', 'ingredients_losses_cook' => '0', 'ingredients_losses_fry' => '0', 'ingredients_losses_stew' => '0', 'ingredients_losses_bake' => '0', ), ), ), 2 => array ( 'barcode' => '', 'category_name' => 'Кофе', 'unit' => 'kg', 'cost' => '15', 'fiscal' => '0', 'hidden' => '0', 'menu_category_id' => '1', 'workshop' => '0', 'nodiscount' => '0', 'photo' => '', 'photo_origin' => NULL, 'price' => array ( 1 => '16681', 2 => '16681', ), 'product_code' => '', 'product_id' => '10', 'product_name' => 'Что то', 'profit' => array ( 1 => '16666', 2 => '16666', ), 'sort_order' => '999', 'tax_id' => '0', 'product_tax_id' => '0', 'type' => '2', 'weight_flag' => '0', 'color' => 'white', 'spots' => array ( 0 => array ( 'spot_id' => '1', 'price' => '16681', 'profit' => '16666', 'visible' => '1', ), 1 => array ( 'spot_id' => '2', 'price' => '16681', 'profit' => '16666', 'visible' => '1', ), ), 'ingredient_id' => '0', 'out' => 0, 'product_production_description' => '', 'ingredients' => array ( 0 => array ( 'structure_id' => '66', 'ingredient_id' => '6', 'pr_in_clear' => '0', 'pr_in_cook' => '0', 'pr_in_fry' => '0', 'pr_in_stew' => '0', 'pr_in_bake' => '0', 'structure_unit' => 'p', 'structure_type' => '1', 'structure_brutto' => 1, 'structure_netto' => 0, 'structure_lock' => '1', 'structure_selfprice' => '15', 'ingredient_name' => 'Крышка 250 мл', 'ingredient_unit' => 'p', 'ingredient_weight' => 0, 'ingredients_losses_clear' => '0', 'ingredients_losses_cook' => '0', 'ingredients_losses_fry' => '0', 'ingredients_losses_stew' => '0', 'ingredients_losses_bake' => '0', ), ), ), ), ) 

How can I process automatically (since there may be a lot of them) array and display all ingredient_name separated by comma in the product card, and at the end with a period?

Gotta get

 <div> Вода, кофе, молоко, Стакан </div> <div> Вода, кофе, молоко, Стакан </div> <div> Что-то, что-то еще, молоко, Стакан </div> 
  • Show the data that should be output - Let's say Pie
  • 2
    Advice to you for the future: instead of print_r() print and publish array dumps with var_export() . The difference is that to work with your published version of the array, you will still need to “finish” it, which is of no interest to anyone, and therefore you can wait longer for the help. - Edward
  • array_column and implode will help you a lot, well, working with cycles. - binliz
  • How many can I ask the same questions about ingredient_name ? - u_mulder

2 answers 2

One option is to bypass the array with array_walk_recursive () :

 $array = array ( 'response' => array ( 0 => array ( 'category_name' => 'Кофе', 'ingredients' => array ( 0 => array ( 0 => array ( 'ingredient_name' => 'Вода', ), 1 => array ( 'ingredient_name' => 'Кофе', ), 2 => array ( 'ingredient_name' => 'Молоко', ), 3 => array ( 'ingredient_name' => 'Бумажные стаканы 250 мл', ), ), 2 => array ( 0 => array ( 'ingredient_name' => 'Вода', ), 1 => array ( 'ingredient_name' => 'Кофе', ), 2 => array ( 'ingredient_name' => 'Молоко', ), 3 => array ( 'ingredient_name' => 'Бумажные стаканы 250 мл', ), ), ), ), ), ); $result = []; array_walk_recursive($array, function($item, $key)use(&$result){ $key != 'ingredient_name' ?: $result[] = $item; }); $result = join(', ', array_unique($result)) . '.'; echo $result; 

Result:

 Вода, Кофе, Молоко, Бумажные стаканы 250 мл. 
  • Displays everything correctly, but does not divide by product cards, and for all displays all the text - Ask Question
  • @ AskThe question from the current context of your question cannot be understood, which cards are we talking about, because You provided only the source array. Try to independently process (substitute its values ​​in the so-called "cards") values ​​of the resulting $ result array. - Edward
  • Each array is a separate product and ingredients must be displayed as Item 1 <div> Water, coffee, milk, Glass </ div> Item 2 <div> Water, coffee, milk, Glass </ div> Item 3 <div> Something , something else, milk, Glass </ div> - Ask Question

Updated handler due to change of data structure.

 // Собираем коллекцию ингридиентов $data = array_reduce($data['response'], function($ret1, $lvl1){ $subItems1 = array_reduce($lvl1['ingredients'], function($ret2, $lvl2){ $ret2[] = $lvl2['ingredient_name']; return $ret2; }, []); $ret1[] = $subItems1; return $ret1; }, []); // Соединяем ингридиенты в разделы $sections = array_map(function($list){ return implode(', ', $list); }, $data); // Ставим последнему точку $sections[] = array_pop($sections) . '.'; // Выводим с экранированием array_walk($sections, function($str){ echo '<div>' . htmlspecialchars($str, ENT_HTML5) . '</div>'; }); 

Result:

 <div>Вода, Кофе, Молоко, Бумажные стаканы 250 мл, Крышка 250 мл, Размешиватель</div> <div>Шоколад черный, Тесто для круассанов</div> <div>Крышка 250 мл.</div> 
  • And if I have such an array (changed the question) - Ask Question
  • Do you want a universal solution based not on the markup of the document, but on ingredients > ingredient_name ? - Total Pusher
  • Yes, I want to process an array of each product and display its ingredients for each product - Ask Question
  • The one that is now the right option, but it can process only one main array - Ask Question
  • Corrected the answer. - Total Pusher