This is my database table which contain actual values我想在PHP中创建动态2D数组。我从我的数据库中获取值。我想在2D数组中添加这些值以便找到核心化。创建Array后,我的输出应该是:
Item1 Item2 Item3 Item4
customeremail rating rating rating rating
customeremail rating rating rating rating
customeremail rating rating rating rating
customeremail rating rating rating rating
这是我的PHP代码,我从数据库中获取值:
<?php
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if ($connection) {
$sql1 = "SELECT item_name FROM feedback GROUP BY item_name";
$result = mysqli_query($connection, $sql1);
while ($row = mysqli_fetch_array($result)) {
$item[]= $row['item_name'].",";
}
for ($i=0;$i<count($item);$i++) {
echo $item[$i]."<br>";
}
mysqli_free_result($result);
$sql = "SELECT cook_email_address FROM feedback GROUP BY cook_email_address";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_array($result)) {
$cookemail[]= $row['cook_email_address'];
}
for ($i=0;$i<count($cookemail);$i++) {
echo $cookemail[$i]."<br>";
}
mysqli_free_result($result);
$sql2 = "SELECT customer_email_address FROM feedback GROUP BY customer_email_address";
$result = mysqli_query($connection, $sql2);
while ($row = mysqli_fetch_array($result)) {
$customeremail[]= $row['customer_email_address'].",";
}
for ($i=0;$i<count($customeremail);$i++) {
echo $customeremail[$i];
}
mysqli_free_result($result);
$sql12 ="SELECT count(*) as total from feedback";
$result = mysqli_query($connection, $sql12);
while ($row=mysqli_fetch_row($result)) {
$count = $row[0];
}
echo $count.'<br>';
for ($i=0;$i<count($cookemail);$i++) {
echo $cookemail[$i];
$sql3 = "SELECT rating FROM feedback WHERE cook_email_address = '$cookemail[$i]'";
$result = mysqli_query($connection, $sql3);
while ($row=mysqli_fetch_array($result)) {
$abc[] = $row['rating'];
}
}
for ($i=0;$i<count($abc);$i++) {
echo $abc[$i];
}
}
?>
我的表名是从我获取所有值的地方的反馈我首先从用户已经给出评级的表中获取项目,之后我已经获取该项目所属的厨师电子邮件或反馈 被送给了。收集这些值后,我已经获取了给出评级的客户电子邮件,最后我获得了在运行时制作2D数组后我必须用于查找核心的评级。
我有制作2D阵列的一般概念但我不知道如何通过制作2D数组来处理我的数据,如果这里有人对我的问题有任何了解,那么请帮助我解决我的问题。
答案 0 :(得分:0)
要立即获取您的数据,您可以在GROUP BY
中使用GROUP_CONCAT
customer_email和SELECT
,以便您的查询如下所示:
SELECT customer_email_address AS customeremail,
GROUP_CONCAT(
DISTINCT CONCAT(customer_email_address, '/', item, '-', rating)
) AS uniqueItem
FROM feedback
GROUP BY customer_email_address
因此,对于您示例中的数据,我们将获得以下数组:
[
'0' => [
'customeremail' => ali@yahoo.com,
'uniqueItem' => 'hani@yahoo.com/bread-4, hani@yahoo.com/palao-5, kamran@yahoo.com/bread-5',
]
]
你仍然需要安排你的数组,但幸运的是它只是一个php数组函数的工作。您可以使用array_map
函数来删除项目名称部分。如果您还有任何疑问,请告诉我,我会更新答案。