1

I Am posting this question as I didn't get a solution while searching here. Here is my question

I have an array coming after submitting data from the view page, its a list of product user has selected to book certain dates(start_date and end_date). like he might have selected 3 different products on the same date, or all the 3 products on different date starting date and return date.

Now I have an array in which I have all the products selected by a user with start_date and end_date and other product related information.

Before Storing these products as ORDERS, I need to check

  1. If multiple products have same start_date and end_date, then it becomes one order.

  2. If multiple products have different start_date and end_date, then it becomes different orders.

Now how will I check if these products have the same start_date and end_date or different?

for example.. this is an array

 Array
(
[0] => Array
    (
        [product_id] => 15
        [start_date] => 2019-05-15
        [end_date] => 2019-05-16
        [status] => in_stock
    )

[1] => Array
    (
        [product_id] => 16
        [start_date] => 2019-05-15
        [end_date] => 2019-05-16
        [status] => out_of_stock
    )

[2] => Array
    (
        [product_id] => 17
        [start_date] => 2019-05-17
        [end_date] => 2019-05-18
        [status] => in_stock
    )
 )

These are 3 different products, here 2 products are booked for same date product_id[15,16]-> so it becomes one Order.

And the other product_id number 17 becomes one second Order.

Edit...

I have added status to the array now, lets say i have 2 orders on same date product_id[15,16], now it has to sort based on two condition,

  1. If multiple order have same start_date and end_date, then it becomes single order/array, which i have achieved by using accepted answer.

  2. Now lets say after sorting i get an array in which i have 2 different products, one is in_stock and other is out_of_stock, now it should again become two different orders.

Please give some solution, as I am stuck here.

Salman Riyaz
  • 808
  • 2
  • 14
  • 37

2 Answers2

2

You can group by the array with the same keys, which in your case is the start and end date. Maybe something like this:

$data = array(); // this is your array
$result = array();
foreach ($data as $element) {
    /* this will create an array that have start and end date as the key. 
    So, order with same start and end date will be grouped as 1.*/
    $result[$element['start_date']." to ".$element['end_date']][] = $element;
}

print_r($result); // do what you want

original codes from this answer.

Afif Zafri
  • 640
  • 1
  • 5
  • 11
  • Thanks, will use and see. – Salman Riyaz Apr 30 '19 at 10:27
  • your answer worked for me, but i have edited my question with one more requirement, can you please look into that? – Salman Riyaz May 20 '19 at 09:00
  • just add the stock status to the key. $result[$element['start_date']." to ".$element['end_date']."_".$element['status']][] = $element; the array key would look like this: "2019-05-15 to 2019-05-16_in_stock" "2019-05-15 to 2019-05-16_out_of_stock" – Afif Zafri May 28 '19 at 03:23
1

array_walk() - Apply a user supplied function to every member of an array

You can use array_walk and iterate through the array to group the arrays by start_date. $arr is your array.

$res = [];
array_walk($arr, function($v, $k) use (&$arr,&$res){
  $nextKey = array_key_exists($k+1, $arr) ? ($k+1) : '';
  if(!empty($nextKey) && 
     $v['start_date'] == $arr[$nextKey]['start_date'] &&
     $v['end_date'] == $arr[$nextKey]['end_date']
  ){
     $res[] = [$v, $arr[$nextKey]];
     unset($arr[$nextKey]);
  }else{
     $res[] = [$v];
  }  
});
echo '<pre>';
print_r($res);

For the total number of order use count($res)

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
  • Thanks for reply, but this will do only for start_date, i wanted for both dates. – Salman Riyaz Apr 30 '19 at 10:26
  • you mean the start_date and end_date both compared to another array? – Rakesh Jakhar Apr 30 '19 at 10:27
  • @RakeshJakhar if the dates continue one it's showing the correct answer, but for order dates with different row it s not showing correctly for example: row 1 startdate :07-05-19 row 2 startdate:08-05-19 row3 startdate: 07-05-19 in this case row 1 and row 3 have the same values but it is showing 3 different dates but if i move row 3 to row2 it is working fine bec it is coming next to next. – sridhar May 07 '19 at 05:14