我试图在foreach循环上运行一个array_search。
$i=0;
foreach(findCustomAlerts($customerid) as $key=>$value){
echo "ID of cat : ".$rifdid[$i++] = $value['Catid']."<br>";
echo "Visits required per day : ".$visitsneeded= $value['Visits required']."<br>";
}
echo "<pre>";
print_r($rifdid);
echo "</pre>";
foreach(getTodaysVisits($device_id) as $rfid){
foreach($rfid as $key=> $read){
//echo $key."<br>";
//$i=0;
if(array_search($key,$rifdid) && $read < $visitsneeded) {
echo $key." has not visited enough";
// $i++;
}
}
}每个循环的第一个数据如下:
Array ( [0] => Array ( [Alertid] => 4 [Catid] => 5609 [Visits required] => 6 ) [1] => Array ( [Alertid] => 5 [Catid] => 23641 [Visits required] => 5 ) )第二阶段的数据如下:
Array ( [rfid_id] => Array ( [23641] => 1 [5609] => 3 ) )我想比较这两个数组,如果rfid标识没有达到当天所需的访问次数,我需要执行另一个操作。
在我运行代码的时候,它只是发现RFID id 23641那天没有进行足够的访问,当它应该发现这两个RFID都没有被看到的时候。
这是当前的输出: cat的ID :每天需要5609次访问:6次cat :每天需要23641次访问:5 23641次访问不够
它还需要输出5609次访问次数不够。
发布于 2020-10-03 17:34:33
看看下面的代码:
您的第一个数组:
$first_arr = Array (
Array (
'Alertid' => 4,
'Catid' => 5609,
'Visits_required' => 6
),
Array (
'Alertid' => 5,
'Catid' => 23641,
'Visits_required' => 5
)
);你的第二个数组;
$second_arr = Array (
'rfid_id' => Array (
23641 => 1,
5609 => 3
)
);首先在第一个数组中创建一个循环,然后在第二个数组中进行循环。匹配catid并检查所需的访问
foreach( $first_arr as $item ){
foreach( $second_arr as $item2 ){
foreach( $item2 as $key => $val ){
if( $key == $item['Catid'] && $val < $item['Visits_required']){
echo $key .' -- not enough visited <br>....<br>';
}
elseif( $key == $item['Catid'] && $val > $item['Visits_required']){
echo $key . ' -- visited enough <br>....<br>';
}
}
}
}它的输出如下:
5609 -- not enough visited
....
23641 -- not enough visited
....我添加了一些点和br使输出在单独的行。您可以使用所需的代码。
https://stackoverflow.com/questions/64186715
复制相似问题