繁体   English   中英

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

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

我有这个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'
 )
)

我想获取此cust_id预订的次数,并想将其添加到其他数组中,我很难确定如何根据cust_id获得每个客户的迭代次数

我想要的输出:

$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
 )
)

我的示例代码:

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

尝试这个:

$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']];
}

我正在跟踪所有id及其在$usedIdsArr使用了多少次。 每次迭代时,我都会检查id是否在$usedIdsArr ,如果不是,则将其添加为1。 如果它在$usedIdsArr ,则增加该值。 然后,将'iteration'的键添加到$data并使用从$usedIdsArr获得的值。

3v4l.org演示

我发现这是一个效率更高的阵列。

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
它是多维的,关键是客户ID。 在每个子阵列中,您可以进行所有预订,并且可以轻松地依靠每个客户。
在您的阵列中,您需要找到每个客户ID的最大值。
我可以echo count($new[52]); 获得“ 52”的预订数量。 您可以从以下代码中获得该代码:

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