转换或重新排列php数组

时间:2018-02-22 21:15:36

标签: php arrays associative-array

我想转换或重新排列这个php数组:

$contacts = array(
array(
    "name" => "Peter Parker",
    "email" => "peterparker@mail.com",
),
array(
    "name" => "Peter Parker", 
    "email" => "petermail2@mail.com",
),
array(
    "name" => "Harry Potter",
    "email" => "harrypotter@mail.com",
)
);

到:

 $contacts ["Peter Parker"] = {"peterparker@mail.com", "petermail2@mail.com"  } ;
 $contacts ["Harry Potter"] = {"harrypotter@mail.com"} ;

表示在新数组或行中组装或合并具有相同名称的条目。

1 个答案:

答案 0 :(得分:0)

foreach( $contacts as $record ){
    $newcontacts[$record['name']][] = $record['email'];
}