首先我得到 SQL 查询结果,并将其放入这样的数组中:
$stmt= $conn->prepare("SELECT p.*, f.*
FROM fruit f
left JOIN person p ON f.fk_p_id = p.p_id
");
$stmt->execute();
$result= $stmt->get_result();
while ($data= $result->fetch_assoc()) {
$arr = array('name' => $data['name'], 'fruit' =>data['fruit']);
$props[] = $arr;
}
var_export($props);
我明白了:
array ( 0 => array (name => CHRISTIAN fruit => apple, ),
1 => array ( name => CHRISTIAN, fruit => pear, ),
2 => array ( name => CHRISTIAN, fruit => strawberry, ),
3 => array ( name => CHRISTIAN, fruit => banana, ),
4 => array ( name => CHRISTIAN, fruit => lime, ),
5 => array ( name => JOSEF, fruit => apple, ),
6 => array ( name => JOSEF, fruit => pear, ),
7 => array ( name => BOB, fruit => apple , ),
8 => array ( name => BOB, fruit => banana, ),)
但是我希望我的数组像这样合并:
array ( 0 => name => CHRISTIAN, fruit => apple , pear, strawberry, banana, lime, ),
1 =>name => JOSEF, fruit => apple, pear, ),
2 =>name => BOB, fruit => apple, banana, ), )
目的是填写这样的表格:
苹果 | 梨子 | 草莓 | 香蕉 | 石灰 | |
---|---|---|---|---|---|
基督教 | x | x | x | x | x |
约瑟夫 | x | x | |||
鲍勃 | x | x |
我应该如何使用多维数组得到这个结果?谢谢
我的假设是你需要一个数组。
如果您尝试以下操作... 您创建一个数组并迭代查询结果。 然后使用 data['name'] 作为键,并将不同的水果作为数组添加到水果键中。
$arr = [];
while ($data= $result->fetch_assoc()) {
$arr[$data['name']]['fruit'][] = $data['fruit'];
}
输出看起来像这样:
Array
(
[CHRISTIAN] => Array
(
[fruit] => Array
(
[0] => apple
[1] => pear
[2] => strawberry
[3] => banana
[4] => lime
)
)
[JOSEF] => Array
(
[fruit] => Array
(
[0] => apple
[1] => pear
)
)
[BOB] => Array
(
[fruit] => Array
(
[0] => apple
[1] => banana
)
)
)
对于桌子,你可以尝试这样的事情:
<table>
<tr>
<th></th>
<th>Apple</th>
<th>Pear</th>
<th>Stawberry</th>
<th>Banana</th>
<th>Lime</th>
</tr>
<?php foreach ($arr as $name => $value) { ?>
<tr>
<td><?php echo $name ?></td>
<td><?php if (in_array('apple', $value['fruit'])) { echo "X"; } ?></td>
<td><?php if (in_array('pear', $value['fruit'])) { echo "X"; } ?></td>
<td><?php if (in_array('strawberry', $value['fruit'])) { echo "X"; } ?></td>
<td><?php if (in_array('banana', $value['fruit'])) { echo "X"; } ?></td>
<td><?php if (in_array('lime', $value['fruit'])) { echo "X"; } ?></td>
</tr>
<?php } ?>
</table>
在我看来,如果您不确切知道会得到哪些成果,您可以从“动态枢纽”中受益。当涉及到动态枢轴时,MySQL 并不是一个优雅的工具(我上次检查过)。
我将按名称对行进行分组,并填充指向空字符串的水果名称关联数组,以便您可以填充表标题并使用这些空字符串作为默认值。
您不仅不需要在循环中调用
fetch()
,您还可以“解构”结果对象有效负载,就好像它是关联数组的数组一样。
代码:(演示)
$defaults = [];
$grouped = [];
foreach ($stmt->get_result() as ['name' => $name, 'fruit' => $fruit]) {
$grouped[$name][$fruit] = 'x';
$defaults[$fruit] = '';
}
$table = <<<HTML
<table border=1>
<tr><th>%s</th></tr>%s
</table>
HTML;
$tableRow = <<<HTML
<tr><td>%s</td></tr>
HTML;
printf(
$table,
implode('</th><th>', array_merge([''], array_keys($defaults))),
implode(
array_map(
fn($name, $row) => sprintf($tableRow, implode('</td><td>', array_merge([$name], $defaults, $row))),
array_keys($grouped),
$grouped
)
)
);
打印 HTML 有点笨拙,因为名称列/数据必须添加到标题行和所有表格主体行之前。
单击演示链接上的眼睛图标即可查看渲染的 HTML 输出。
您可以使用此库https://github.com/updatablejs/ujb来组装查询结果。您需要一个像“p.f*(fruits)”这样的结构。 “fruit”表中的实体将添加到“person”表的相应实体中。 结构“f*(fruits)”的这一部分表示对于“person”表中的每个实体,我们可以从“fruit”表中拥有多个实体,并且这些实体将被添加到“fruits”中的数组中领域。
$result = $database->prepare(
'SELECT p.*, f.* FROM fruit f
left JOIN person p ON f.fk_p_id = p.p_id'
)
->setAssembler([
'structure' => 'p.f*(fruits)',
])
->execute();
print_r($result->fetchAll());
我还没有测试查询,但结果应该是以下形式:
Array
(
[0] => Array
(
[person_id] => 1
[name] => CHRISTIAN
[fruits] => Array
(
[0] => Array
(
[fruit_id] => 1
[person_id] => 1
[name] => apple
)
[1] => Array
(
[fruit_id] => 2
[person_id] => 1
[name] => pear
)
[2] => Array
(
[fruit_id] => 3
[person_id] => 1
[name] => strawberry
)
)
)
[1] => Array
(
[person_id] => 2
[name] => JOSEF
[fruits] => Array
(
[0] => Array
(
[fruit_id] => 4
[person_id] => 2
[name] => apple
)
[1] => Array
(
[fruit_id] => 5
[person_id] => 2
[name] => pear
)
)
)
)