У меня есть массивы:
'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',
),
),
),
),
)
Как я могу обработать автоматически (так как их может быть много) массив и вывести все ingredient_name через запятую в карточку товара, и в конце с точкой?
Должен получить
<div>
Вода, кофе, молоко, Стакан
</div>
<div>
Вода, кофе, молоко, Стакан
</div>
<div>
Что-то, что-то еще, молоко, Стакан
</div>
Один из вариантов - обойти массив с помощью 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;
Результат:
Вода, Кофе, Молоко, Бумажные стаканы 250 мл.
Обновил обработчик в связи со сменой структуры данных.
// Собираем коллекцию ингридиентов
$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>';
});
Результат:
<div>Вода, Кофе, Молоко, Бумажные стаканы 250 мл, Крышка 250 мл, Размешиватель</div>
<div>Шоколад черный, Тесто для круассанов</div>
<div>Крышка 250 мл.</div>
Айфон мало держит заряд, разбираемся с проблемой вместе с AppLab
Перевод документов на английский язык: Важность и ключевые аспекты
Есть такая функция, которая разбивает строку на части заданной длинны:
Вообщем такая проблема, есть проект на yii2 там есть категории с бесконечной вложенностью и такая задача вывести все товары из данной под категории...
Не удается спарсить данные с https://anistarme/