我正在尝试将自定义字段添加到我的演示平台,并具有以下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
)
)
我想要实现的是这样我可以遍历自定义字段并将它们添加到我的视图模板中:
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。
非常感谢任何帮助。
答案 0 :(得分:0)
瞧瞧! 见Demo
代码:
$res_non_associative = 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
)
);
// You won't have to do that - Proccess only coz i couldn't get the
// associative array from fetchAll()
$res = array();
array_push($res, array_values($res_non_associative[0]));
array_push($res, array_values($res_non_associative[1]));
// Array that will be returned
$final_array = array();
// Depending how the result will be put in array
// Regarding your query it seems you catched 2 by 2
// Therefore we need a cursor to add into the final_array
$cursor = 0;
for($i = 0; $i < count($res); $i += 2)
{
// We will push into main array all infos except "customfields"
$main_array = array();
$max_main_insert = count($res[$i]) - 2;
for($j = 0; $j < $max_main_insert; ++$j)
{
array_push($main_array, $res[$i][$j]);
}
array_push($final_array, $main_array);
// We will push into custom array all the "customfield"
$custom_array = array();
array_push($custom_array, $res[$cursor][$max_main_insert]);
array_push($custom_array, $res[$cursor][$max_main_insert +1 ]);
array_push($custom_array, $res[$cursor + 1][$max_main_insert]);
array_push($custom_array, $res[$cursor + 1][$max_main_insert +1 ]);
array_push($final_array[$i], $custom_array);
++$cursor;
}
print_r($final_array);
输出:
Array
(
[0] => Array
(
[0] => John Doe
[1] => john.doe@example.com
[2] => 0123456789
[3] => esljqpmdh
[4] => Great presentation
[5] => 2015-06-05 14:17:15
[6] => How to make a great presentation.
[7] => Doe John
[8] => doe.john@example.com
[9] => Array
(
[0] => Favourite Colour
[1] => Blue
[2] => Age
[3] => 26
)
)
)
答案 1 :(得分:0)
你对关联数组好吗?然后创建一个新数组(比如说$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'];
}