0

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"
  ]
]
Trijex
  • 17
  • 6
  • Could you just use a simple `if(!in_array())` before pushing `Carbon::createFromFormat()` to `$pastSales[$historySale['brand_id']][]`? – Tim Lewis May 13 '22 at 19:50
  • @TimLewis Unfortunately i get undefined offset error since the array i am pushing to is empty – Trijex May 13 '22 at 20:56
  • This is confusing. Is the first array before you process it? Can you show what the output from `SaleHistoryCount::all()` looks like in a manner that is conducive to copy and paste? – miken32 May 13 '22 at 21:31
  • There's almost certainly a cleaner way to do this using collection methods; using `toArray()` is almost never required. – miken32 May 13 '22 at 21:37
  • `in_array()` doesn't use array indices to check for the value, it looks for it in the values of the array... I'm not sure what you mean by *"I get undefined offset error [...]"*; maybe if you [edited your question](https://stackoverflow.com/posts/72234593/edit) to show how using `in_array()` is triggering that error, then we'd be able to help further... But miken32 is also correct; you shouldn't need to call `->toArray()` to do manipulation when Laravel's Collections have similar methods: https://laravel.com/docs/9.x/collections#available-methods vs https://www.php.net/manual/en/ref.array.php – Tim Lewis May 14 '22 at 14:10

1 Answers1

0

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;

    }
}
Jared
  • 1,294
  • 2
  • 8
  • 13