简体   繁体   English

计算forid中每个id的迭代次数

[英]Count the number of iterations per id's in foreach

I have this associative array 我有这个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 我想获取此cust_id预订的次数,并想将其添加到其他数组中,我很难确定如何根据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++;
   } 
}

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 . 我正在跟踪所有id及其在$usedIdsArr使用了多少次。 Each iteration I check if the id is in $usedIdsArr , if not, I add it with a value of one. 每次迭代时,我都会检查id是否在$usedIdsArr ,如果不是,则将其添加为1。 If it is in $usedIdsArr , I increment the value. 如果它在$usedIdsArr ,则增加该值。 Then I add the key of 'iteration' to $data with the value I got from $usedIdsArr . 然后,将'iteration'的键添加到$data并使用从$usedIdsArr获得的值。

3v4l.org demo 3v4l.org演示

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 https://3v4l.org/dpl2C
it's multidimensional and the key is the customer id. 它是多维的,关键是客户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. 在您的阵列中,您需要找到每个客户ID的最大值。
I can just echo count($new[52]); 我可以echo count($new[52]); to get the number of bookings for "52". 获得“ 52”的预订数量。 You can get that from this code: 您可以从以下代码中获得该代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM