0

嗨,我有这样的数组:

0 => 
    array (size=6)
      'id' => string '55' (length=2)
      'uid' => string '1' (length=1)
      'item_id' => string '5' (length=1)
      'item_name' => string 'dfgdfg' (length=6)
      'price' => string '14.91' (length=5)
      'time' => string '1373975023' (length=10)
  1 => 
    array (size=6)
      'id' => string '59' (length=2)
      'uid' => string '1' (length=1)
      'item_id' => string '5' (length=1)
      'item_name' => string 'dfgdfg' (length=6)
      'price' => string '14.91' (length=5)
      'time' => string '1373978756' (length=10)
  2 => 
    array (size=6)
      'id' => string '58' (length=2)
      'uid' => string '1' (length=1)
      'item_id' => string '6' (length=1)
      'item_name' => string 'kubek #1' (length=8)
      'price' => string '10.01' (length=5)
      'time' => string '1373978751' (length=10)
  3 => 
    array (size=6)
      'id' => string '56' (length=2)
      'uid' => string '1' (length=1)
      'item_id' => string '7' (length=1)
      'item_name' => string 'ewr' (length=3)
      'price' => string '1.05' (length=4)
      'time' => string '1373975032' (length=10)

我需要把这个数组变成类似的东西:

      0 => 
    array (size=6)
      'id' => string '59' (length=2)
      'uid' => string '1' (length=1)
      'item_id' => string '5' (length=1)
      'item_name' => string 'dfgdfg' (length=6)
      'price' => string '14.91' (length=5)
      'time' => string '1373978756' (length=10)
      'q' => 2 

  1 => 
    array (size=6)
      'id' => string '58' (length=2)
      'uid' => string '1' (length=1)
      'item_id' => string '6' (length=1)
      'item_name' => string 'kubek #1' (length=8)
      'price' => string '10.01' (length=5)
      'time' => string '1373978751' (length=10) 
      'q' => 1 
  2 => 
    array (size=6)
      'id' => string '56' (length=2)
      'uid' => string '1' (length=1)
      'item_id' => string '7' (length=1)
      'item_name' => string 'ewr' (length=3)
      'price' => string '1.05' (length=4)
      'time' => string '1373975032' (length=10) 
      'q' => 1

需要计算有多少特定值放入数组并将它们放入新的数组中。此刻我尝试以这种方式执行此操作,但效果不佳

foreach ($b as $k => $v) {
        $bas[$k]['q'] = array_count_values($v['item_id']);
    }
4

1 回答 1

1

如果您使用的是数据库,则可以使用以下查询:

 SELECT *, count(item_id) AS quantity FROM table GROUP BY item_id
于 2013-07-16T13:12:38.630 回答