PHP检查并替换值是否存在于多数组中?

时间:2019-03-18 02:10:48

标签: php arrays multidimensional-array

问题:

我不知道/不知道如何检查日期和地点是否在同一“行”上,并且它们是否存在一次以上。 其次,我该如何合并数组 我的案例带有ArraySchedule的MergeArray

代码:

$ArraySchedule = array();
while ($data = $stmt -> fetch(PDO::FETCH_ASSOC)) {

    $schedules = array(
        "id" => $data['id'],
        "name" => $data['name'],
        "date" => $data['date'],
        "time" => $data['time'],
        "place_id" => $data['place_id'],
        "place" => $data['place'],


    );
    array_push($ArraySchedule, $schedules);

}
$dupe_array = array();
foreach ($ArraySchedule as $key => $value) {
   if(++$dupe_array[$value["date"]] > 1 && ++$dupe_array[$value["place_id"]] > 1 ){
    // this statement is wrong, i want something like: 
    // if date and place_id exists on the same "row" and they exists more then once 
 }
}

我想做什么:

检查ArraySchedule是否包含具有相同日期和位置的计划, 如果有多个时间表具有相同的date和place_id。 那么我想用这种结构更新ArraySchedule

$MergeArray = array(
        "id" => ArraySchedule['id'],
        "name" => array(
            "name" => scheduleSameDateAndPlace['name'],
            "name" => scheduleSameDateAndPlace['name'],
            "name" => scheduleSameDateAndPlace['name'],
        ),
        "date" => $ArraySchedule['date'],
        "time" => $ArraySchedule['time'],
        "place_id" => $ArraySchedule['place_id'],
        "place_name" => $ArraySchedule['place_name'],
    ),

MergeArray与ArraySchedule? 无论如何...

我想得到的输出?

Print_r($ArraySchedule) 
        array(
            [0] => 
                array(
                    [id] => 1 
                    [names] => Simon 
                    [date] => 2019-01-02 
                    [time] 18.00 
                    [place_id] => Tystberga Park 
                    [place] => Tystberga
                )
            [1] => 
                array(
                    [id] => 2 
                    //[names] insted of [name]?
                    [names] => 
                        array(
                            [name] => Vincent 
                            [name] => Angel 
                            [name] => Kim
                        )
                    [date] => 2019-02-17
                    [time] => 13.00
                    [place_id] => Borås Park
                    [place] => Borås
                )
            [2] => 
                array(
                    [id] => 3
                    // [names] is always an array?
                    [names] => Caitlyn 
                    [date] => 2019-03-15 
                    [time] 13.00 
                    [place_id] => Plaza Park 
                    [place] => EvPark
                )
        ) 

2 个答案:

答案 0 :(得分:1)

您可以使用array-reduce。请考虑以下内容:

function mergeByDateAndPlace($carry, $item) {
    $key = $item["place_id"] . $item["date"]; // creating key matching exact place and date
    if (!isset($carry[$key])) {
        $carry[$key]["name"] = $item["name"]; 
    } else {
        $carry[$key] = $item; 
        $item["name"] = [$item["name"]]; // make default array with 1 element so later can be append other names
    }
    return $carry;
}

现在将其用于:

$MergeArray = array_reduce($ArraySchedule, "mergeByDateAndPlace", []);

如果您以后想知道是否有重复项,可以在$MergeArray上循环。如果要舍弃连动键,也可以使用array_values

@Nick 2条有关保存第一个循环和需要确定的“时间”值的重要评论。还要注意,您的期望输出包含具有相同键(“名称”)的多元素-您需要在它们后面附加int键-数组不能有重复的键。

希望有帮助!

答案 1 :(得分:0)

这是我数据库中的数据:

var_export($ ArraySchedule)

array ( 
 0 => array ( 'id' => '225', 'place_id' => 'Alviks Kulturhus', 'name' => 'BarraBazz', 'date' => '2019-03-19', 'placeadress' => 'Gustavslundsvägen 1', ), 
 1 => array ( 'id' => '229', 'place_id' => 'Axelhuset Göteborg', 'name' => 'Anders Björk', 'date' => '2019-04-08', 'placeadress' => 'Axel Dahlströms torg 3', ), 
 2 => array ( 'id' => '230', 'place_id' => 'Axelhuset Göteborg', 'name' => 'Black Jack', 'date' => '2019-04-08', 'placeadress' => 'Axel Dahlströms torg 3', ), 
 3 => array ( 'id' => '227', 'place_id' => 'Arosdansen Syrianska Kulturcentret', 'name' => 'BarraBazz', 'date' => '2019-05-08', 'placeadress' => 'Narvavägen 90', ), 
 4 => array ( 'id' => '228', 'place_id' => 'Aspåsnäset', 'name' => 'Blender', 'date' => '2019-05-25', 'placeadress' => 'Aspåsnäset 167', ), 
 5 => array ( 'id' => '226', 'place_id' => 'Arenan Västervik Resort', 'name' => 'Blender', 'date' => '2019-06-29', 'placeadress' => 'Lysingsvägen', ), 
 6 => array ( 'id' => '222', 'place_id' => 'Alingsåsparken', 'name' => 'Bendéns', 'date' => '2019-07-16', 'placeadress' => 'Folkparksgatan 3A', ), 
 7 => array ( 'id' => '223', 'place_id' => 'Alingsåsparken', 'name' => 'Charlies', 'date' => '2019-07-16', 'placeadress' => 'Folkparksgatan 3A', ), 
 8 => array ( 'id' => '224', 'place_id' => 'Allhuset Södertälje', 'name' => 'Cedrix', 'date' => '2019-07-16', 'placeadress' => 'Barrtorpsvägen 1A', ), )

每次place_id和日期相同时,我想用名称数组更新“名称”。

这是我想要的输出:

Array ( 
[0] => 
    Array ( [id] => 225 [place_id] => Alviks Kulturhus [name] => BarraBazz [date] => 2019-03-19 [placeadress] => Gustavslundsvägen 1 ) 
[1] => 
    Array ( [id] => 229 [place_id] => Axelhuset Göteborg [name] => Array([0] => Anders Björk [1] => Black Jack )  [date] => 2019-04-08 [placeadress] => Axel Dahlströms torg 3 ) 
[3] => 
    Array ( [id] => 227 [place_id] => Arosdansen Syrianska Kulturcentret [name] => BarraBazz [date] => 2019-05-08 [placeadress] => Narvavägen 90 ) 
[4] => 
    Array ( [id] => 228 [place_id] => Aspåsnäset [name] => Blender [date] => 2019-05-25 [placeadress] => Aspåsnäset 167 ) 
[5] => 
    Array ( [id] => 226 [place_id] => Arenan Västervik Resort [name] => Blender [date] => 2019-06-29 [placeadress] => Lysingsvägen ) 
[6] => 
    Array ( [id] => 222 [place_id] => [Alingsåsparken] [name] => Array([0] => Bendéns [1] => Charlies) [date] => 2019-07-16 [placeadress] => Folkparksgatan 3A ) 
[8] =>
    Array ( [id] => 224 [place_id] => Allhuset Södertälje [name] => Cedrix [date] => 2019-07-16 [placeadress] => Barrtorpsvägen 1A ) )

这是我更新的代码

$sql = "SELECT `schedule`.`id`,`schedule`.`place_id`,`schedule`.`name`,`schedule`.`date`,`places`.`placeadress` FROM `schedule` INNER JOIN `places` ON `schedule`.`place_id`=`places`.`place_id` ORDER BY `date`";
$stmt = $db -> prepare($sql);
$stmt -> execute();
$ArraySchedule = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_export($ArraySchedule);
$DatePlace = array();
    foreach ($ArraySchedule as $key => $Schedule){

        $Arrayquery = "SELECT `schedule`.`id`,`schedule`.`place_id`,`schedule`.`name`,`schedule`.`date`,`places`.`placeadress` FROM `schedule` INNER JOIN `places` ON `schedule`.`place_id`=`places`.`place_id` WHERE `schedule`.`date`= :date_ AND `schedule`.`place_id` = :place_id ORDER BY `date`";
        $ArrayStmt = $db->prepare($Arrayquery);
        $ArrayStmt -> execute(array(":date_" => $Schedule['date'],":place_id" => $Schedule['place_id']));
        //Getting every $Schedule that has the same date and place_id
        if($ArrayStmt->rowCount() > 1){

            //Here i want two update the name inside 
            //$ArrayArraySchedule with an array of names 
            //that has the same place and date?


        }
    }
print_r($ArraySchedule);