PHP检查,如果数组值是每个ID阵列超过previous值小 [英] PHP check if value in array is smaller than previous value in array per ID
问题描述
我有我的地方存储一个ID,并为每个ID有保存开始和结束值的数组的数组。我的目标是,以检查是否启动值大于该项目的最终值,该数组中的前谈到小。
I have an array where I store an ID and for every ID there's an array that holds a start and end value. My goal is to check if the start value is smaller than the end value of the item that comes before it in the array.
这工作,但我的循环不关心ID的,我需要一种方法来做到这一点每个ID
This works but my loop doesn't care about the ID's, I need a way to do this per ID
下面就是我的数组是这样的:
Here's what my array looks like:
Array
(
[892] => Array
(
[41] => Array
(
[0] => Array
(
[start] => 0930
[end] => 1200
)
)
)
[897] => Array
(
[41] => Array
(
[0] => Array
(
[start] => 1000
[end] => 1230
)
)
)
[898] => Array
(
[41] => Array
(
[0] => Array
(
[start] => 1100
[end] => 1300
)
)
)
[901] => Array
(
[52] => Array
(
[0] => Array
(
[start] => 0930
[end] => 1030
)
)
)
[903] => Array
(
[52] => Array
(
[0] => Array
(
[start] => 0930
[end] => 1030
)
)
)
[904] => Array
(
[41] => Array
(
[0] => Array
(
[start] => 1000
[end] => 1230
)
)
)
)
这是我的循环是什么样子:
And this is what my loop looks like:
$foundOverlap = false;
$prevEnd = null;
foreach($slots as $s){
$startHour = $s[0]['start'];
$endHour = $s[0]['end'];
echo $startHour . '<br>';
echo $endHour . '<br>';
if($prevEnd !== null && $startHour < $prevEnd){
$foundOverlap = true;
$counter++;
}
if($foundOverlap){
echo 'overlap <br>';
}else{
echo 'no overlap <br>';
}
$prevEnd = $endHour;
}
结果我没有得到任何重叠(好),重叠(好),重叠(好),重叠(BAD!)这应该是没有重叠。但它说的重叠,因为它与previous数组项检查不关心attractieID是什么...
As a result I get No overlap(good), overlap(good), overlap(good), overlap(BAD!) this should be no overlap. But it says overlap because It checks with the previous array item doesn't care what the attractieID is...
请人知道我怎样才能解决这个问题?
Anyone please knows how I can fix this?
我会很感激SOOOO!
I would be soooo thankful!!
在此先感谢!
推荐答案
从穆罕默德·阿巴西Kashif的评论,我想给你一个解决方案
From Muhammad Kashif Abbasi's comment I would like to give you a solution
试试这个code
$foundOverlap = false;
$prevEnd = null;
$preID = null;
foreach($slots as $s){
$startHour = $s[0]['start'];
$endHour = $s[0]['end'];
$currentID = $s['attractieID']; //got the current attractieID
echo $startHour . '<br>';
echo $endHour . '<br>';
if($prevEnd !== null && $startHour < $prevEnd && $currentID == $preID){ // checked current id and previous id
$foundOverlap = true;
$counter++;
}
if($foundOverlap){
echo 'overlap <br>';
}else{
echo 'no overlap <br>';
}
$foundOverlap = false; //reseted $foundOverlap
$prevEnd = $endHour;
$preID = $currentID; //set the current id as previous id
}
这篇关于PHP检查,如果数组值是每个ID阵列超过previous值小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!