【发布时间】:2018-09-05 07:55:29
【问题描述】:
我有一个项目,其中包含具有一些给定选项的过滤器列表。每个选项都应该包含下一个父母的孩子。所以基本上整个数组必须从头到尾解析,但我不确定如何在 PHP 中做到最好。
初始列表如下所示:
Model
- Paper basic
- Paper standard
Format
- 12x7x3 cm
- 15x15x5 cm
Color
- White
- Blue
- Red
在视觉上,我需要做的是:
<table border="1" style="width: 100%;">
<tr><td>Paper basic</td><td>12x7x3 cm</td><td>White</td></tr>
<tr><td></td><td></td><td>Blue</td></tr>
<tr><td></td><td></td><td>Red</td></tr>
<tr><td></td><td>15x15x5 cm</td><td>White</td></tr>
<tr><td></td><td></td><td>Blue</td></tr>
<tr><td></td><td></td><td>Red</td></tr>
<tr><td>Paper standard</td><td>12x7x3 cm</td><td>White</td></tr>
<tr><td></td><td></td><td>Blue</td></tr>
<tr><td></td><td></td><td>Red</td></tr>
<tr><td></td><td>15x15x5 cm</td><td>White</td></tr>
<tr><td></td><td></td><td>Blue</td></tr>
<tr><td></td><td></td><td>Red</td></tr>
</table>
PHP Array:
$parents = array();
$parent = array();
$parent['id'] = 1;
$parent['name'] = 'Model';
$parent['slug'] = 'model';
$parent['type'] = 'filter';
$parent['children'] = array();
$child = array();
$child['id'] = 1;
$child['name'] = 'Paper basic';
$child['slug'] = 'paper-basic';
$child['type'] = 'option';
array_push($parent['children'], $child);
$child = array();
$child['id'] = 2;
$child['name'] = 'Paper standard';
$child['slug'] = 'paper-standard';
$child['type'] = 'option';
array_push($parent['children'], $child);
array_push($parents, $parent);
$parent = array();
$parent['id'] = 2;
$parent['name'] = 'Format';
$parent['slug'] = 'format';
$parent['type'] = 'filter';
$parent['children'] = array();
$child = array();
$child['id'] = 3;
$child['name'] = '12x7x3 cm';
$child['slug'] = '12x7x3-cm';
$child['type'] = 'option';
array_push($parent['children'], $child);
$child = array();
$child['id'] = 4;
$child['name'] = '15x15x5 cm';
$child['slug'] = '15x15x5-cm';
$child['type'] = 'option';
array_push($parent['children'], $child);
array_push($parents, $parent);
$parent = array();
$parent['id'] = 3;
$parent['name'] = 'Color';
$parent['slug'] = 'color';
$parent['type'] = 'filter';
$parent['children'] = array();
$child = array();
$child['id'] = 5;
$child['name'] = 'White';
$child['slug'] = 'white';
$child['type'] = 'option';
array_push($parent['children'], $child);
$child = array();
$child['id'] = 6;
$child['name'] = 'Blue';
$child['slug'] = 'blue';
$child['type'] = 'option';
array_push($parent['children'], $child);
$child = array();
$child['id'] = 7;
$child['name'] = 'Red';
$child['slug'] = 'red';
$child['type'] = 'option';
array_push($parent['children'], $child);
array_push($parents, $parent);
echo '<pre>';
print_r($parents);
echo '</pre>';
数组结果:
Array
(
[0] => Array
(
[id] => 1
[name] => Model
[slug] => model
[type] => filter
[children] => Array
(
[0] => Array
(
[id] => 1
[name] => Paper basic
[slug] => paper-basic
[type] => option
)
[1] => Array
(
[id] => 2
[name] => Paper standard
[slug] => paper-standard
[type] => option
)
)
)
[1] => Array
(
[id] => 2
[name] => Format
[slug] => format
[type] => filter
[children] => Array
(
[0] => Array
(
[id] => 3
[name] => 12x7x3 cm
[slug] => 12x7x3-cm
[type] => option
)
[1] => Array
(
[id] => 4
[name] => 15x15x5 cm
[slug] => 15x15x5-cm
[type] => option
)
)
)
[2] => Array
(
[id] => 3
[name] => Color
[slug] => color
[type] => filter
[children] => Array
(
[0] => Array
(
[id] => 5
[name] => White
[slug] => white
[type] => option
)
[1] => Array
(
[id] => 6
[name] => Blue
[slug] => blue
[type] => option
)
[2] => Array
(
[id] => 7
[name] => Red
[slug] => red
[type] => option
)
)
)
)
我尝试了类似的方法,但它仅适用于下一项,但下一项应包含下一项的子项:
for ($j = 0; $j <= count($parents); $j++)
{
if (isset($parents[$j+1]['children']))
{
array_push($parents[$j]['children'], $parents[$j+1]['children']);
}
}
【问题讨论】:
-
请附上您迄今为止尝试过的代码!以及源数组(最好从
var_export($yourarray)解析) -
对于给定的数组,您基本上需要 3 个嵌套的 foreach 循环:一个用于“模型”。
$parents[0]['children'],一种用于“格式”$parents[1]['children'],一种用于“颜色”$parents[2]['children'] -
问题是不仅有 3 个 foreach 循环,它可以是无限数量的父母。我尝试的是这个(见上文,最新代码)