我有一个来自数据库查询的数组:
Array
(
[0] => Array
(
[distance] => 14928
[end] => 5
[id] => 12
)
[1] => Array
(
[distance] => 15645
[end] => 2
[id] => 1
)
和这样的数组:
$locations = array(
2,3,5
);
我想要实现的目标如下:
循环遍历$locations
数组,执行查询并删除等于$dbarray[0]['end']
的整数,然后再次执行foreach而不使用之前的未设置值。
所以在这种情况下:
$locations before loop = 2,3,5
$locations after loop = 2,3 (because the "end" of the first result is 5)
与#34; new"再次$locations
到目前为止我所拥有的:
$locations = array(
2,3,5
);
foreach ($locations as $key => $location) {
$query = $em->createQuery(
'SELECT l,l.distance,l.end,l.id
FROM AppBundle:Point l
WHERE l.start = :startPoint
AND l.end IN (:endPoint)
ORDER BY l.distance ASC'
)
->setParameter('startPoint', 1)
->setParameter('endPoint', $locations);
$result = $query->getResult();
unset($locations[$result[0]['end']]);
echo $result[0]['end']."<br />";
但echo $result[0]['end']
仅提供&#34; 5&#34;在每一行。
我认为我的问题是,查询中的$locations
始终是&#34; 2,3,5&#34;,不是。什么是正确的方法?
答案 0 :(得分:0)
完整解决方案:
$start = 1;
$locations = array(
2,3,5,4
);
echo "<pre>";
$em = $this->getDoctrine()->getManager($this->getUser()->getDbuser());
foreach ($locations as $key => &$location) {
$query = $em->createQuery(
'SELECT l,l.distance,l.end,l.id
FROM AppBundle:Point l
WHERE l.start = :startPoint
AND l.end IN (:endPoint)
ORDER BY l.distance ASC'
)
->setParameter('startPoint', 1)
->setParameter('endPoint', $locations);
$result = $query->getResult();
// Remove from array
$locations = array_diff($locations,array($result[0]['end']));
echo $result[0]['end']."<br />";
}
// Last one in the array
echo $result[1]['end']."<br />";
可能不是最干净的方式,但对我有用。