JJ D. Lordes
JJ D. Lordes

Reputation: 91

Count the number of iterations per id's in foreach

I have this associative array

$data = array(
  0=>array(
    'id'=>1,
    'cust_id'=>51,
    'roomtype'=>'PREMIUM',
    'start'=>'2018-12-20',
    'end'=>'2018-12-25',
  ),
  1=>array(
    'id'=>2,
    'cust_id'=>51,
    'roomtype'=>'PRESIDENTIAL',
    'start'=>'2018-12-26',
    'end'=>'2019-01-01'
 ),
 2=>array(
    'id'=>3,
    'cust_id'=>52,
    'roomtype'=>'PREMIUM',
    'start'=>'2019-01-08',
    'end'=>'2019-'01-12'
 )
 3=>array(
    'id'=>4,
    'cust_id'=>52,
    'roomtype'=>'DELUXE',
    'start'=>'2019-01-13',
    'end'=>'2019-'01-20'
 ),
 4=>array(
    'id'=>5,
    'cust_id'=>53,
    'roomtype'=>'DOUBLE',
    'start'=>'2019-01-13',
    'end'=>'2019-'01-25'
 )
)

I wanted to get the number of times this cust_id had booked, and I wanted to add it in my other array, Im having a hard time as to how am I gonna get the number of iteration per customer based on the cust_id

My desired output:

$new = array(
  0=>array(
    'id'=>1,
    'cust_id'=>51,
    'roomtype'=>'PREMIUM',
    'start'=>'2018-12-20',
    'end'=>'2018-12-25',
    'iteration'=>1
  ),
  1=>array(
    'id'=>2,
    'cust_id'=>51,
    'roomtype'=>'PRESIDENTIAL',
    'start'=>'2018-12-26',
    'end'=>'2019-01-01',
    'iteration'=>2
 ),
 2=>array(
    'id'=>3,
    'cust_id'=>52,
    'roomtype'=>'PREMIUM',
    'start'=>'2019-01-08',
    'end'=>'2019-'01-12',
    'iteration'=>1
 )
 3=>array(
    'id'=>4,
    'cust_id'=>52,
    'roomtype'=>'DELUXE',
    'start'=>'2019-01-13',
    'end'=>'2019-'01-20',
    'iteration'=>2
 ),
 4=>array(
    'id'=>5,
    'cust_id'=>53,
    'roomtype'=>'DOUBLE',
    'start'=>'2019-01-13',
    'end'=>'2019-'01-25',
    'iteration'=>1
 )
)

My sample code:

$i=1;
$new = array();
foreach ($data as $key=>$value) {
   if ($value['cust_id'] == $value['cust_id']) {
    $new[$key]['iteration']
    $new[$key] = $value;
    $i++;
   } 
}

Upvotes: 0

Views: 62

Answers (2)

Andreas
Andreas

Reputation: 23958

I find this a much more efficient array.

array (
  51 => 
  array (
    0 => 
    array (
      'id' => 1,
      'cust_id' => 51,
      'roomtype' => 'PREMIUM',
      'start' => '2018-12-20',
      'end' => '2018-12-25',
    ),
    1 => 
    array (
      'id' => 2,
      'cust_id' => 51,
      'roomtype' => 'PRESIDENTIAL',
      'start' => '2018-12-26',
      'end' => '2019-01-01',
    ),
  ),
  52 => 
  array (
    0 => 
    array (
      'id' => 3,
      'cust_id' => 52,
      'roomtype' => 'PREMIUM',
      'start' => '2019-01-08',
      'end' => '2019-01-12',
    ),
    1 => 
    array (
      'id' => 4,
      'cust_id' => 52,
      'roomtype' => 'DELUXE',
      'start' => '2019-01-13',
      'end' => '2019-01-20',
    ),
  ),
  53 => 
  array (
    0 => 
    array (
      'id' => 5,
      'cust_id' => 53,
      'roomtype' => 'DOUBLE',
      'start' => '2019-01-13',
      'end' => '2019-01-25',
    ),
  ),
)

https://3v4l.org/dpl2C
it's multidimensional and the key is the customer id. Inside each subarray you have all the bookings and can easily count on each customer.
In your array you need to find the maximum value of each customer id.
I can just echo count($new[52]); to get the number of bookings for "52". You can get that from this code:

foreach($data as $sub){
    $new[$sub['cust_id']][]= $sub;
}
var_export($new);

Upvotes: 0

Ethan
Ethan

Reputation: 4375

Try this:

$usedIdsArr = [];
foreach ($data as $key => $row) {
    if (!array_key_exists($row['cust_id'], $usedIdsArr)) {
        $usedIdsArr[$row['cust_id']] = 1;
    } else {
        $usedIdsArr[$row['cust_id']]++;
    }
    $data[$key]['iteration'] = $usedIdsArr[$row['cust_id']];
}

I'm tracking all the ids and how many times they're used in $usedIdsArr. Each iteration I check if the id is in $usedIdsArr, if not, I add it with a value of one. If it is in $usedIdsArr, I increment the value. Then I add the key of 'iteration' to $data with the value I got from $usedIdsArr.

3v4l.org demo

Upvotes: 2

Related Questions