I have an array from a DB query:
Array
(
[0] => Array
(
[distance] => 14928
[end] => 5
[id] => 12
)
[1] => Array
(
[distance] => 15645
[end] => 2
[id] => 1
)
and an array like this:
$locations = array(
2,3,5
);
What I want to achieve is the following: Loop through the $locations
array, do the query and delete integer which is equal to $dbarray[0]['end']
, then do the foreach again without the unset value from before. So in this case:
$locations before loop = 2,3,5
$locations after loop = 2,3 (because the "end" of the first result is 5)
foreach with the "new" $locations
again
What I have so far:
$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 />";
But echo $result[0]['end']
gives only "5" in every row.
I think my problem is, that the $locations
in the query are always "2,3,5", isn't it. What is the right approach?
Full solution:
$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 />";
Probably not the cleanest way, but works for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.