重构一个数组

时间:2011-04-04 18:22:21

标签: php amfphp

我在SQL查询的AMFPHP中得到以下结果:

(Array)#0
  [0] (Object)#1
    id = "1"
    naam = "4 kazen"
    pizzeria_id = "1"
    pizzerianaam = "Howest pizza"
    prijs = "11"
  [1] (Object)#2
    id = "2"
    naam = "Napolitano"
    pizzeria_id = "1"
    pizzerianaam = "Howest pizza"
    prijs = "13"
  [2] (Object)#3
    id = "5"
    naam = "pizza bolognaise"
    pizzeria_id = "2"
    pizzerianaam = "Nicola pizza"
    prijs = "12"
  [3] (Object)#4
    id = "6"
    naam = "pizza funghi"
    pizzeria_id = "2"
    pizzerianaam = "Nicola pizza"
    prijs = "16"

我想要做的是重构这个数组,所以结果是一个内部有2个数组的assosiative数组,2个数组应该在“pizzerianaam”之后调用,而在“pizzerianaam”中我应该看到披萨的位置“pizzerianaam”比赛。

所以一个包含2个数组的数组,其中包含“pizzerianaam”值匹配的每个披萨的其他数组。

任何有帮助的人?

1 个答案:

答案 0 :(得分:0)

如果查询结果存储在$array

$result = array();
foreach ($array as $item)
{
  $result[$item->pizzerianaam][] = $item;
}

然后生成的数组将是$result