1

我有这种类型的关联数组。

Array
(
 [0] => Array
    (
        [bookedArea] => Comp Pool
        [laneBooked] => 1
        [paidBy] => Edmonton Aurora Synchronized Swim Club
        [startTime24hr] => 16:00
        [finishTime24hr] => 18:00
    )

 [1] => Array
    (
        [bookedArea] => Comp Pool
        [laneBooked] => 2
        [paidBy] => Edmonton Aurora Synchronized Swim Club
        [startTime24hr] => 16:00
        [finishTime24hr] => 18:00
    )

 [2] => Array
    (
        [bookedArea] => Comp Pool
        [laneBooked] => 3
        [paidBy] => Keyano Swim Club
        [startTime24hr] => 16:00
        [finishTime24hr] => 18:00
    )
)

我想创建一个新数组,根据多个关联删除类似条目。startTime24hr、finishTime24hrbookedArea 和paidBy 如果所有 4 个都不相同,则不应将其删除。另外,我想捕获新关联数组中的第一条和最后一条通道。

Array
  (
    [0] => Array
     (
        [bookedArea] => Comp Pool
        [firstLaneBooked] => 1
        [lastLaneBooked] => 2
        [paidBy] => Edmonton Aurora Synchronized Swim Club
        [startTime24hr] => 16:00
        [finishTime24hr] => 18:00
     )
    [2] => Array
     (
        [bookedArea] => Comp Pool
        [firstLaneBooked] => 3
        [lastLaneBooked] => 3
        [paidBy] => Keyano Swim Club
        [startTime24hr] => 16:00
        [finishTime24hr] => 18:00
     )
  )

我发现这可以帮助我只过滤一个关联。但是我还需要3个。

$copy = $array; // create copy to delete dups from the array you wish to filter from
$newFilteredArray = array(); // new filtered array.
$item1 = 'startTime24hr'; // the association you want to filter with.
for( $i=0; $i<count($array); $i++ ) {
    if ( in_array( $array[$i][$item1], $newFilteredArray ) ) {
        unset($copy[$i]);
    }
    else {
        $newFilteredArray [] = $array[$i][$item1]
    }
}
print_r($copy);

编辑----谢谢亚历山大帮助我得到正确的迭代。

我一直在努力将开始和结束车道插入我的过滤数组。我已经注释掉了我让它发挥作用的尝试。当它未注释时,我得到:Notice: Undefined index: startTime24hr 它似乎处于无限循环中。但是,如果您评论我的东西,则没有通知。有人能帮忙吗?函数 array_push_assoc($array, $key, $value){ $array[$key][] = $value; 返回$数组;}

$CPfilteredArray = array();
$lanes = 0;
for($i = 0; $i < count($compPoolArray); $i++) {
  $add = true;
  // $lanesAdded = false;
  // $lanes = $lanes + 1;
  // $startLane = $compPoolArray[$i]["laneBooked"];
  for($j = 0; $j < count($CPfilteredArray); $j++) {
    if(($compPoolArray[$i]["startTime24hr"] === $CPfilteredArray[$j]["startTime24hr"])
        && ($compPoolArray[$i]["finishTime24hr"] === $CPfilteredArray[$j]["finishTime24hr"])
        && ($compPoolArray[$i]["bookedArea"] === $CPfilteredArray[$j]["bookedArea"])
        && ($compPoolArray[$i]["paidBy"] === $CPfilteredArray[$j]["paidBy"])) {
            $add = false;
            // $lanes = $lanes + 1;
            // $lanesAdded = True;
    }
  }
    if($add) {
        $CPfilteredArray[] = $compPoolArray[$i];
        // if ($lanesAdded){
        //     $lastLane = $startLane + $lanes;
        //     $CPfilteredArray[$i] = array_push_assoc($CPfilteredArray, 'firstLane', $startLane);
        //     $CPfilteredArray[$i] = array_push_assoc($CPfilteredArray, 'lastLane', $lastLane);
        // }
        // else {
        //     $CPfilteredArray[$i] = array_push_assoc($CPfilteredArray, 'firstLane', $startLane);
        //     $CPfilteredArray[$i] = array_push_assoc($CPfilteredArray, 'lastLane', $startLane);
        // }
    }
}
4

4 回答 4

1

这也可能有效:(您可以替换序列化函数以仅序列化选定的元素)

<?php 

$arr=array(
        0=>array("a"=>1, "b"=>"hello"),
        1=>array("a"=>2, "b"=>"hellooo"),
        2=>array("a"=>1, "b"=>"hello"),
        );

function removeDuplicates( &$arr ){
    $index=array();
    $result=array();
    foreach ($arr as $key=>$val){
        $k=serialize($val);
        if(!isset($index[$k])){
            $index[$k]=$key;
            $result[$key]=$val;
        }
    }
    return $result;
}

$result=removeDuplicates($arr);

print_r($arr);
print '<br>';
print_r($result);
?>
于 2012-04-03T20:15:36.887 回答
1

两个循环循环完成这项工作。

$filteredArray = array();
for($i = 0; $i < count($array); $i++) {
  $add = true;
  for($j = 0; $j < count($filteredArray); $j++) {
    if(($array[$i]["startTime24hr"] === $filteredArray[$j]["startTime24hr"])
        && ($array[$i]["finishTime24hr"] === $filteredArray[$j]["finishTime24hr"])
        && ($array[$i]["bookedArea"] === $filteredArray[$j]["bookedArea"])
        && ($array[$i]["paidBy"] === $filteredArray[$j]["paidBy"])) {
       $add = false;
    }
  }
  if($add) $filteredArray[] = $array[$i];
}

调用的结果数组$filteredArray包含过滤后的元素。

于 2012-04-03T20:05:52.447 回答
1

我知道它并没有真正回答(不允许发表评论......)你的问题,但如果你从数据库中检索数据,你可以使用查询来检索分组值。它可能看起来像这样(伪SQL)

SELECT
     bookedArea
    ,paidBy
    ,startTime24hr
    ,finishTime24hr
    ,MIN(laneBooked)
    ,MAX(laneBooked)
FROM 
    reservation -- ??
WHERE
--condition
GROUP BY
    bookedArea
    ,paidBy
    ,startTime24hr
    ,finishTime24hr
于 2012-04-03T20:08:07.720 回答
1

如果您确实知道用于合并数组值的元素,您还可以使用哈希图在插入时进行合并。因此,一旦将元素添加到数组中,您就会创建一个散列

$bookings = array();
$hash = md5($arr['bookedArea'] . $arr['paidBy'] . $arr['startTime24hr'] . $arr['finishTime24hr']);
if ( array_key_exists($hash, $bookings) ){
    //add just another lane to the booking
}
else{
    //create a new booking entry
}

这样你的代码可能会更有效率。

于 2012-04-03T20:13:40.400 回答