PHP Array下一项作为子项

时间:2018-09-05 07:55:29

标签: php arrays arraylist

我有一个项目,其中包含具有某些给定选项的过滤器列表。每个选项都应包含下一个父母的孩子。因此,基本上必须从头到尾解析整个数组,但是我不确定用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']);
      }
   }

3 个答案:

答案 0 :(得分:1)

我想出了一个不需要所有父数组都嵌套循环的解决方案。只需为行中的单元格嵌套循环并为尽可能多的父母工作即可。利用模来确定每一行的内容。

$total_rows = 1;
foreach($parents as $parent)
{
    $total_rows *= count($parent["children"]);
}


$rows = '';
for($row = 0;$row < $total_rows;$row++)
{
    $cells = '';
    $count_product = 1;
    for($col = count($parents) - 1;$col >= 0;$col--)
    {
        $parent = $parents[$col];
        $cells = '<td>' . ((($row % $count_product) == 0) ? $parent["children"][($row/$count_product)%count($parent["children"])]["name"] : "") . '</td>' . $cells;
        $count_product *= count($parent["children"]);
    }
    $rows .= '<tr>'.$cells.'<tr>';
}

echo '<table>'.$rows.'</table>';

答案 1 :(得分:0)

这是一些(直到现在很丑)的代码,可以满足您的需求:

echo "<table border=1>";
foreach($parents[0]['children'] as $model) {
    echo "<tr>";
    echo "<td>".$model['name']."</td>";
    $firstFormatDone=false;
    foreach($parents[1]['children'] as $format) {
        if($firstFormatDone) {
            echo "<tr><td></td>";
        }
        echo "<td>".$format['name']."</td>";
        $firstFormatDone=true;
        $firstColorDone=false;

        foreach($parents[2]['children'] as $color) {
            if($firstColorDone) {
                echo "<tr><td></td><td></td>";
            }
            echo "<td>".$color['name']."</td>";
            $firstColorDone=true;
            echo "<tr>";
        }
    }
}
echo "</table>";

正如您所看到的,应该将一种模式放入返回函数中(因为您现在说的是,层次结构的数量是固定的)

答案 2 :(得分:0)

我个人不喜欢彼此之间有一对foreaches

也许您正在为此寻找如果不是,请告诉我。

$parents = [];

$parent = [
    'id'       => 1,
    'name'     => 'Model',
    'slug'     => 'model',
    'type'     => 'filter',
    'children' => [],
];

$child = [
    'id'   => 1,
    'name' => 'Paper basic',
    'slug' => 'paper-basic',
    'type' => 'option',
];

array_push($parent['children'], $child);

$child = [
    'id'   => 2,
    'name' => 'Paper standard',
    'slug' => 'paper-standard',
    'type' => 'option',
];

array_push($parent['children'], $child);
array_push($parents, $parent);

$parent = [
    'id'       => 2,
    'name'     => 'Format',
    'slug'     => 'format',
    'type'     => 'filter',
    'children' => [],
];

$child = [
    'id'   => 3,
    'name' => '12x7x3 cm',
    'slug' => '12x7x3-cm',
    'type' => 'option',
];

array_push($parent['children'], $child);

$child = [
    'id'   => 4,
    'name' => '15x15x5 cm',
    'slug' => '15x15x5-cm',
    'type' => 'option',
];

array_push($parent['children'], $child);
array_push($parents, $parent);

$parent = [
    'id'       => 3,
    'name'     => 'Color',
    'slug'     => 'color',
    'type'     => 'filter',
    'children' => [],
];

$child = [
    'id'   => 5,
    'name' => 'White',
    'slug' => 'white',
    'type' => 'option',
];

array_push($parent['children'], $child);

$child = [
    'id'   => 6,
    'name' => 'Blue',
    'slug' => 'blue',
    'type' => 'option',
];

array_push($parent['children'], $child);

$child = [
    'id'   => 6,
    'name' => 'Red',
    'slug' => 'red',
    'type' => 'option',
];

array_push($parent['children'], $child);
array_push($parents, $parent);

foreach ($parents as $parent)
{
    foreach ($parent as $child)
    {
        if (is_array($child))
        {
            foreach ($child as $result)
            {
                echo '<pre>';
                var_dump($result);
                echo '</pre>';
            }
        }
    }
}

结果:

array(4) {
  ["id"]=>
  int(1)
  ["name"]=>
  string(11) "Paper basic"
  ["slug"]=>
  string(11) "paper-basic"
  ["type"]=>
  string(6) "option"
}
array(4) {
  ["id"]=>
  int(2)
  ["name"]=>
  string(14) "Paper standard"
  ["slug"]=>
  string(14) "paper-standard"
  ["type"]=>
  string(6) "option"
}
array(4) {
  ["id"]=>
  int(3)
  ["name"]=>
  string(9) "12x7x3 cm"
  ["slug"]=>
  string(9) "12x7x3-cm"
  ["type"]=>
  string(6) "option"
}
array(4) {
  ["id"]=>
  int(4)
  ["name"]=>
  string(10) "15x15x5 cm"
  ["slug"]=>
  string(10) "15x15x5-cm"
  ["type"]=>
  string(6) "option"
}
array(4) {
  ["id"]=>
  int(5)
  ["name"]=>
  string(5) "White"
  ["slug"]=>
  string(5) "white"
  ["type"]=>
  string(6) "option"
}
array(4) {
  ["id"]=>
  int(6)
  ["name"]=>
  string(4) "Blue"
  ["slug"]=>
  string(4) "blue"
  ["type"]=>
  string(6) "option"
}
array(4) {
  ["id"]=>
  int(6)
  ["name"]=>
  string(3) "Red"
  ["slug"]=>
  string(3) "red"
  ["type"]=>
  string(6) "option"
}

与您的HTML有关。您可以执行此操作

foreach ($parents as $parent)
{
    foreach ($parent as $child)
    {
        if (is_array($child))
        {
            foreach ($child as $result): ?>
                <tr>
                    <td><?php echo $result['name'] ;?></td>
                    <td><?php echo $result['slug'] ;?></td>
                    <td><?php echo $result['type'] ;?></td>
                </tr>
            <?php endforeach;
        }
    }
}

文档: