gpt4 book ai didi

php - PDO 查询 fetchAll 删除键但保留包括重复项的值

转载 作者:行者123 更新时间:2023-11-30 22:42:30 25 4
gpt4 key购买 nike

我正在尝试将自定义字段添加到我的演示平台并进行以下 MySQL/PDO 查询:

SELECT presenters.presenter_name,
presenters.presenter_email,
presenters.presenter_contact,
presentations.presentation_uid,
presentations.presentation_presenter_notes,
presentations.presentation_date,
presentations.presentation_customer_reference,
presentations.presentation_customer_name,
presentations.presentation_customer_email,
customfields.customfield_name,
customfields_data.customfield_data_value
FROM presentations
INNER JOIN presenters ON presentations.presentation_presented_by = presenters.presenter_id
LEFT JOIN customfields ON customfields.customfield_presentation_uid = presentations.presentation_uid
LEFT JOIN customfields_data ON customfields_data.customfield_data_id = customfields.customfield_id
WHERE presentations.presentation_uid = :presentation_id

我使用 $presentation = $query->fetchAll(PDO::FETCH_ASSOC); 执行查询,然后 print_r 结果并得到以下结果:

Array (
[0] => Array
(
[presenter_name] => John Doe
[presenter_email] => john.doe@example.com
[presenter_contact] => 0123456789
[presentation_uid] => esljqpmdh
[presentation_presenter_notes] => Great presentation
[presentation_date] => 2015-06-05 14:17:15
[presentation_customer_reference] => How to make a great presentation.
[presentation_customer_name] => Doe John
[presentation_customer_email] => doe.john@example.com
[customfield_name] => Favourite Colour
[customfield_data_value] => Blue
)

[1] => Array
(
[presenter_name] => John Doe
[presenter_email] => john.doe@example.com
[presenter_contact] => 0123456789
[presentation_uid] => esljqpmdh
[presentation_presenter_notes] => Great presentation
[presentation_date] => 2015-06-05 14:17:15
[presentation_customer_reference] => How to make a great presentation.
[presentation_customer_name] => Doe John
[presentation_customer_email] => doe.john@example.com
[customfield_name] => Age
[customfield_data_value] => 26
)
)

我想要实现的是这样我可以遍历自定义字段并将它们添加到我的 View 模板中:

Array (
[presenter_name] => John Doe
[presenter_email] => john.doe@example.com
[presenter_contact] => 0123456789
[presentation_uid] => esljqpmdh
[presentation_presenter_notes] => Great presentation
[presentation_date] => 2015-06-05 14:17:15
[presentation_customer_reference] => How to make a great presentation.
[presentation_customer_name] => Doe John
[presentation_customer_email] => doe.john@example.com
[customfield_name] => Favourite Colour
[customfield_data_value] => Blue
[customfield_name] => Age
[customfield_data_value] => 26
)

或者更好:

Array (
[presenter_name] => John Doe
[presenter_email] => john.doe@example.com
[presenter_contact] => 0123456789
[presentation_uid] => esljqpmdh
[presentation_presenter_notes] => Great presentation
[presentation_date] => 2015-06-05 14:17:15
[presentation_customer_reference] => How to make a great presentation.
[presentation_customer_name] => Doe John
[presentation_customer_email] => doe.john@example.com
[customfields] => Array (
[customfield_name] => Favourite Colour
[customfield_data_value] => Blue
[customfield_name] => Age
[customfield_data_value] => 26
)
)

但我不确定下一步该怎么做,我完全卡住了,不确定我的代码的哪一部分不正确。查询或 fetchAll。

如有任何帮助,我们将不胜感激。

最佳答案

你接受关联数组吗?然后创建一个新数组(比如 $def_presentation 或其他)并进行类似

的操作
foreach($presentation as $row) {
if(!isset($def_presentation[$row['presentation_uid']])) {
$def_presentation[$row['presentation_uid']] = $row;
unset($def_presentation[$row['presentation_uid']]['customfield_name'],$def_presentation[$row['presentation_uid']]['customfield_data_value']);
$def_presentation[$row['presentation_uid']]['customfields'] = array();
}
$def_presentation[$row['presentation_uid']]['customfields'][$row['customfield_name']] = $row['customfield_data_value'];
}

关于php - PDO 查询 fetchAll 删除键但保留包括重复项的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30733622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com