-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