array(6) {
[0]=>
array(2) {
["title"]=>
string(23) "Товар 1"
["theme"]=>
string(5) "Кастрюли"
}
[1]=>
array(2) {
["title"]=>
string(23) "Товар 2"
["theme"]=>
string(5) "Кастрюли"
}
[2]=>
array(2) {
["title"]=>
string(20) "Товар 3"
["theme"]=>
string(14) "Сковородки"
}
[3]=>
array(2) {
["title"]=>
string(20) "Товар 4"
["theme"]=>
string(14) "Сковородки"
}
[4]=>
array(2) {
["title"]=>
string(31) "Товар 5"
["theme"]=>
string(6) "Кружки"
}
[5]=>
array(2) {
["title"]=>
string(31) "Товар 6"
["theme"]=>
string(6) "Кружки"
}
}
<b>Кастрюли</b>
<ul>
<li>Товар 1
<li>Товар 2
</ul>
<b>Сковородки</b>
<ul>
<li>Товар 3
<li>Товар 4
</ul>
<b>Кружки</b>
<ul>
<li>Товар 5
<li>Товар 6
</ul>
<?php
$list = []; // ваш массив
$p = [];
foreach ($list as $item) {
if (!isset($p[$item['theme']])) {
$p[$item['theme']] = [];
}
$p[$item['theme']][] = $item['title'];
}
/*
* теперь $p будет иметь вид:
*
* [
* 'Кастрюли' => ['Товар1', 'Товар3', 'Товар2', ]
* 'Чашки' => ['Чашка 1', 'Чашка 2']
* ]
*
* дальше сами
* foreach ($p as $title => $items) {
* foreach ($items as $item) {
* // и т д
* }
* }
*/
<?php
$products = [
[
'title' => 'Товар 1',
'theme' => 'Кастрюли',
],
[
'title' => 'Товар 2',
'theme' => 'Кастрюли',
],
[
'title' => 'Товар 3',
'theme' => 'Сковородки',
],
[
'title' => 'Товар 4',
'theme' => 'Сковородки',
],
[
'title' => 'Товар 5',
'theme' => 'Кружки',
],
[
'title' => 'Товар 6',
'theme' => 'Кружки',
],
];
$categories = [];
foreach ($products as $product) {
$categories[$product['theme']][] = $product['title'];
}
foreach ($categories as $category => $products) {
echo "<b>$category</b>";
echo "<ul>";
foreach ($products as $product) {
echo "<li>$product</li>";
}
echo "</ul>";
}
$groupped = array_reduce($arr, function ($current, $item) {
$current[$item['theme']][] = $item['title'];
return $current;
}, []);
foreach ($groupped as $title => $items) {
echo "<b>$title<b/>";
echo "<ul>";
foreach ($items as $item) {
echo "<li>$item</li>";
}
echo "</ul>";
}