根据迭代键值

时间:2016-07-14 08:50:02

标签: php arrays loops sorting

我有以下数组输出:

array (size=102)
  0 => 
    object(stdClass)
      public 'MapId' => string '1' (length=1)
      public 'Id'    => string '1' (length=1)
  1 => 
    object(stdClass)
      public 'MapId' => string '1' (length=1)
      public 'Id'    => string '2' (length=1)
  3 => 
    object(stdClass)
      public 'MapId' => string '2' (length=1)
      public 'Id'    => string '3' (length=1)
  4 => 
    object(stdClass)
      public 'MapId' => string '2' (length=1)
      public 'Id'    => string '4' (length=1)

现在我想要做的是获得MapId 1 MapId的所有数组迭代,然后所有迭代的2 array_map() while等等。

我不确定是否应该为foreach创建函数,在DECLARE @Test TABLE ( ID NVARCHAR(10),Number INT ) INSERT INTO @Test VALUES ('TG32',6) INSERT INTO @Test VALUES ('TG32',6) INSERT INTO @Test VALUES ('TG32',6) INSERT INTO @Test VALUES ('TG32',2) INSERT INTO @Test VALUES ('QD65',2) INSERT INTO @Test VALUES ('QD65',3) INSERT INTO @Test VALUES ('QD65',3) INSERT INTO @Test VALUES ('HT76',7) INSERT INTO @Test VALUES ('HT76',1) INSERT INTO @Test VALUES ('HT76',5) DECLARE @Categories TABLE ( ID NVARCHAR(10), Item NVARCHAR(10) ) INSERT INTO @Categories (ID, Item) VALUES ( 'TG32', 'Hats' ), ( 'QD65', 'Coats' ), ( 'HT76', 'Shoes' ), ( 'WR52', 'Shirts' ) SELECT C.Item, ISNULL(S.Number, 0) AS 'Number' FROM @Categories C LEFT JOIN @Test S ON C.ID = S.ID 循环中使用SELECT C.Item, SUM(ISNULL(S.Number, 0)) AS 'Number' FROM @Categories C LEFT JOIN @Test S ON C.ID = S.ID GROUP BY C.Item 循环,或者PHP是否具有简单的本机函数可以做这样的事情。

有人可以说明如何做到这一点吗?

0 个答案:

没有答案