2

Problem:

I dont know/understand how to check if date and place exists on the same "row" and they exists more then once. Second, how do i then merge an array my case MergeArray with ArraySchedule

Code:

$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 
 }
}

What i want to do:

Check if ArraySchedule contains schedules that have the same date and place, if there is more than one schedule that has the same date and place_id. then I want to update ArraySchedule with this structure

$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 with ArraySchedule? anyway...

Output I think I want?

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
                )
        ) 
6
  • 2
    Your entire first loop can be replaced with $ArraySchedule = $stmt->fetchAll(PDO::FETCH_ASSOC); Commented Mar 18, 2019 at 2:15
  • 'Output I think I want?' Very good start posting your expected result... It would be helpful as well to provide the source array example as var_export to allow copy'n'paste since people don't want to build data structures by typing by hand. Commented Mar 18, 2019 at 2:25
  • 2
    What if two events have the same date and place but a different time? Commented Mar 18, 2019 at 2:31
  • 1
    What about the IDs? You show that you use an arbitrary one out of the matching meetings, and the result IDs are a sequence 1,2,3. When reordering IDs, the numeric array key would be enough. You need to declare how IDs are to be handled. Commented Mar 18, 2019 at 2:37
  • @Avoka94 Did my post helped you? Commented Mar 18, 2019 at 14:00

2 Answers 2

1

You can use array-reduce. Consider the following:

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;
}

Now use it with:

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

If you later want to know if there were any duplicate you can just loop on $MergeArray. You can also use array_values if you want to discard the concat keys.

Notice @Nick 2 important comment about saving the first loop and the "time" value that need to be decided. Also notice your desire output contain multi element with the same key ("name") - you need to append them with int key - Array can not have duplicate keys.

Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

0

Here is my data from my database:

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', ), )

I want to update the "name" with an array of names everytime that place_id and date are the same.

This is the output I want:

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 ) )

Here is my updated code

$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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.