-2
我有一个PHP数组是这样的:如何获得这样的数组?
Array
(
[0] => Array
(
[name] => abc
[id] => 107
[CycleNumber] => 1
[Type] => People
)
[1] => Array
(
[name] => john
[id] => 312
[CycleNumber] => 5
[Type] => People
)
[2] => Array
(
[name] => jenny
[id] => 110
[CycleNumber] => 3
[Type] => People
)
[3] => Array
(
[name] => metting room
[id] => 590
[CycleNumber] => 4
[Type] => Facility
)
[4] => Array
(
[name] => projector
[id] => 470
[CycleNumber] => 4
[Type] => Facility
)
)
我想用空白来代替重复的类型。但第一个应该有这个类型的名称,其他的应该是空白的。所以结果数组应该是这样的:
Array
(
[0] => Array
(
[name] => abc
[id] => 107
[CycleNumber] => 1
[Type] => People
)
[1] => Array
(
[name] => john
[id] => 312
[CycleNumber] => 5
[Type] =>
)
[2] => Array
(
[name] => jenny
[id] => 110
[CycleNumber] => 3
[Type] =>
)
[3] => Array
(
[name] => metting room
[id] => 590
[CycleNumber] => 4
[Type] => Facility
)
[4] => Array
(
[name] => projector
[id] => 470
[CycleNumber] => 4
[Type] =>
)
)
我只想要这种格式的数组。我正在使用PHP zend。
我搜索这个,但其中大部分显示从数组中删除该元素。但我不想删除它。我想用空白替换它,但想显示第一个。
试过代码
$result = array();
$result1 = array();
$result2 = array();
$y = array();
$y1 = array();
foreach ($data as $entry) {
$type= $entry["type"];
if (!isset($y[$type])) {
$y[$type] = array();
unset($entry["type"]);
$result[$type][] = $entry;
}
}
谁能告诉我该怎么做?
你尝试过什么?这不是一个难题,但答案将取决于你的尝试。 – Tigger 2013-04-29 03:26:00
我试过这个,但它会改变数组格式,所以我不能使用它。 $ result = array(); $ result1 = array(); \t \t \t $ result2 = array(); \t \t \t $ y = array(); \t \t \t $ y1 = array(); \t \t \t的foreach($数据作为$条目){\t \t \t \t \t \t \t $类型= $条目[ “型”]; \t \t \t \t if(!isset($ y [$ type]))$ y [$ type] = array(); \t \t \t \t unset($ entry [“type”]); \t \t \t \t \t \t \t \t \t \t \t $结果[$类型] [] = $条目; \t \t \t \t \t} – Mausami 2013-04-29 03:33:39
使用您当前的代码更新问题,并包含您可能获得的任何错误消息。 – Tigger 2013-04-29 04:09:13