我得到了一些數據集,它必須被映射,分組重複,像這樣:PHP數據映射
我:
<?php
$var = array(
array("Name" => "Alfred", "Number" => 1),
array("Name" => "Alfred", "Number" => 2),
array("Name" => "Alfred", "Number" => 3),
array("Name" => "Aethelstan", "Number" => 4),
array("Name" => "Aethelstan", "Number" => 5),
array("Name" => "Aethelstan", "Number" => 6),
)
?>
我想將它更改爲:
<?php
$var = array(
array("name" => "Alfred","Number" => array(1,2,3)),
array("name" => "Aelthestan","Number" => array(4,5,6)),
)
?>
有人有沒有很多循環和一對一比較的想法?
如果名稱是有效的標識符,您可以創建一個數組,其中姓名是鍵,並且數字值的數組。如果不是,您可以使用散列函數,並將其用作關鍵字。 (並將名稱存儲在數組中)。 – Dan