Skip to content
Advertisement

php Keeping only 1 date occurence in multi-dimensional array

I have this array with certain brand_ids, within these brands I have an array of dates in which a sale occured but these are based on the products in sale so they may appear multiple times on the same brand_id;

This is my array:

array:5 [▼
  2 => array:3 [▼
    0 => "2022-05-08"
    1 => "2022-05-08"
    2 => "2022-05-08"
  ]
  3 => array:5 [▼
    0 => "2022-05-08"
    1 => "2022-05-08"
    2 => "2022-05-08"
    3 => "2022-05-08"
    4 => "2022-05-08"
  ]
  4 => array:1 [▼
    0 => "2022-05-08"
  ]
  1 => array:3 [▼
    0 => "2022-05-01"
    1 => "2022-05-08"
    2 => "2022-05-08"
  ]
  6 => array:3 [▼
    0 => "2022-05-08"
    1 => "2022-05-08"
    2 => "2022-05-08"
  ]
]

The code to generate this :

pastSales = [];

$historySales = SaleHistoryCount::all()->toArray();

foreach($historySales as $key => $historySale) {
    $saleDateToCompare = Carbon::createFromFormat('Y-m-d H:i:s', $historySale['sale_date'])
        ->format('Y-m-d');

    if(in_array($saleDateToCompare , $saleDays)) {
        if(! isset($pastSales[$historySale['sale_date']])) {
            $pastSales [$historySale['brand_id']][] = Carbon::createFromFormat('Y-m-d H:i:s', $historySale['brand_id'])
                ->format('Y-m-d');
        }
    }
}

$saleDays is a 2D array of every sunday untill a certain year like so

[
    "2022-05-08"
    "2022-05-15"
    "2022-05-22"
]

All the duplicates stripped out and have it reduced to one unless the date is different per brand_id but I can’t seem to be able to produce that with array_unique, array_mapping and/or array_columns… How would I achieve the output below?

array:5 [▼
  2 => array:3 [▼
    0 => "2022-05-08"
  ]
  3 => array:5 [▼
    0 => "2022-05-08"
  ]
  4 => array:1 [▼
    0 => "2022-05-08"
  ]
  1 => array:3 [▼
    0 => "2022-05-01"
    2 => "2022-05-08"
  ]
  6 => array:3 [▼
    0 => "2022-05-08"
  ]
]

Advertisement

Answer

Use in_array as Tim Lewis proposed:

foreach($historySales as $key => $historySale) {
    $saleDateToCompare = Carbon::createFromFormat('Y-m-d H:i:s', $historySale['sale_date'])
        ->format('Y-m-d');

    if(in_array($saleDateToCompare , $saleDays)) {

        $date_formatted = Carbon::createFromFormat('Y-m-d H:i:s', $historySale['brand_id'])->format('Y-m-d');

        // !!! Is this string correct? Maybe we should check for "$historySale['brand_id']" existance?
        if(! isset($pastSales[$historySale['sale_date']]))
            $pastSales[$historySale['brand_id']] = [];

        if( !in_array($date_formatted, $pastSales[$historySale['brand_id']]) )
            $pastSales[$historySale['brand_id']][] = $date_formatted;

    }
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement