我正在努力研究如何从 MySQL 查询构建多维数组。并同时删除重复值。以下是查询的输出。 可以看出,索引 3 和 4 是第二个维度的候选者。下面的代码实现了这一点,
但是我在尝试合并第 1 维重复索引 3 和 4 时遇到问题。因此 pets 数组包含两个宠物。
foreach ($Q1RS as $key => $val) {
$Result[$key]['OwnerID'] = $val['OwnerID'];
$Result[$key]['Client'] = $val['Owner'];
$Result[$key]['Since'] = $val['Since'];
(!is_null($val['Pets? $Result[$key]['Pets'][] = $val['Kitchen']: '';
}
Extract from the resulting array
3 =>
array (size=4)
'OwnerID' => string '4' (length=1)
'Owner' => string 'Rosie' (length=14)
'Since' => string '1212-12-12' (length=10)
'Pets' =>
array (size=1)
0 => string 'Poodle' (length=9)
4 =>
array (size=4)
'OwnerID' => string '4' (length=1)
'Owner' => string 'Rosie' (length=14)
'Since' => string '1212-12-12' (length=10)
'Pets' =>
array (size=1)
0 => string 'Pony' (length=9)
MySQL result output
array (size=8)
0 =>
array (size=4)
'OwnerID' => string '1' (length=1)
'Owner' => string 'Bob' (length=7)
'Since' => string '2012-08-01' (length=10)
'pets' => null
1 =>
array (size=4)
'OwnerID' => string '2' (length=1)
'Owner' => string 'Julie' (length=19)
'Since' => string '2007-07-07' (length=10)
'pets' => null
2 =>
array (size=4)
'OwnerID' => string '3' (length=1)
'Owner' => string 'Laing O'Rourke' (length=14)
'Since' => string '1212-12-12' (length=10)
'pets' => null
3 =>
array (size=4)
'OwnerID' => string '4' (length=1)
'Owner' => string 'Rosie' (length=14)
'Since' => string '1212-12-12' (length=10)
'pets' => string 'Poodle' (length=9)
4 =>
array (size=4)
'OwnerID' => string '4' (length=1)
'Owner' => string 'Rosie' (length=14)
'Since' => string '1212-12-12' (length=10)
'pets' => string 'Pony' (length=9)
5 =>
array (size=4)
'OwnerID' => string '5' (length=1)
'Owner' => string 'Gary' (length=10)
'Since' => string '1212-12-12' (length=10)
'pets' => null
6 =>
array (size=4)
'OwnerID' => string '6' (length=1)
'Owner' => string 'Sarah' (length=9)
'Since' => string '1212-12-12' (length=10)
'pets' => null
7 =>
array (size=4)
'OwnerID' => string '7' (length=1)
'Owner' => string 'The Owner' (length=10)
'Since' => string '2021-07-31' (length=10)
'pets' => null
表格列如下 所有者 -> ID、姓名、姓氏、地址、手机、电子邮件、自从、 宠物 -> ID、宠物、日期、所有者 ID
$Q1SQL = 'SELECT
o.ID AS OwnerID,
CONCAT(Name, ' ', Surname) AS o.Owner,
o.Since,
p.Pets
FROM owners AS o
LEFT JOIN pets AS p
ON o.ID = p.OwnerID';
$Statement = $DBConnection->query($Q1SQL);
$Q1RS = $Statement->fetchAll(PDO::FETCH_ASSOC);
if (!$Q1RS) {
die('Query 1 failed!');
}
重现问题的数据
DROP TABLE IF EXISTS `owners`;
CREATE TABLE `owners` (
`ID` mediumint unsigned NOT NULL AUTO_INCREMENT,
`Name` varchar(50) NOT NULL DEFAULT '',
`Surname` varchar(50) NOT NULL DEFAULT '',
`Address` varchar(75) NOT NULL DEFAULT '',
`Phone` varchar(13) NOT NULL DEFAULT '',
`Email` varchar(50) NOT NULL DEFAULT '',
`Since` date NOT NULL DEFAULT '1212-12-12',
PRIMARY KEY (`ID`),
UNIQUE KEY `Email` (`Email`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC;
#
# Data for table "owners"
#
INSERT INTO `owners` VALUES (4,'Rosie','Smith','','0134 123 123','Rosie@gmail.com','2020-12-12'),(8,'Gary','Stevens','','0134 123 123','Gary@hotmail.com','2020-12-12'),(10,'Sarah','Smith','','0134 123 123','Sarah@hotmail.com','2020-12-12');
#
# Structure for table "pets"
#
DROP TABLE IF EXISTS `pets`;
CREATE TABLE `pets` (
`ID` mediumint unsigned NOT NULL AUTO_INCREMENT,
`Pet` varchar(30) NOT NULL DEFAULT '',
`Date` date NOT NULL DEFAULT '1212-12-12',
`OwnerID` mediumint unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
KEY `OwnerID` (`OwnerID`),
CONSTRAINT `pets_ibfk_1` FOREIGN KEY (`OwnerID`) REFERENCES `owners` (`ID`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC;
#
# Data for table "pets"
#
INSERT INTO `pets` VALUES (1,'Shetland Pony','2021-01-01',4),(2,'Jack Russel','2021-01-01',10),(3,'Poodle','2021-01-01',4);