比较存在于同一多维数组中的2个数组

时间:2011-05-16 00:55:36

标签: php arrays multidimensional-array

我有一个关联数组:

Array
(
[0] => Array
    (
        [0] => Array
            (
                [0] => name=>Jose
                [1] => email=>jo@example.com
            )

        [1] => Array
            (
                [0] => name=>Adriana
                [1] => email=>add@example.com
            )

    )


[1] => Array
    (
        [0] => Array
            (
                [0] => name=>Jose
                [1] => email=>juniper@example.com
            )

        [1] => Array
            (
                [0] => name=>Maria
                [1] => email=>maria@example.com
            )
    )
)

我想收集所有成员的所有电子邮件地址。 Jose有2个电子邮件地址,Maria 1和Adriana 1.如何比较多维数组中共存的2个数组?

我想要类似的东西:

Array
(
[0] => Array
(
[0] => name=>Jose
[1] => firstemail=>jo@example.com
[2] => secondemail=>juniper@example.com
)

[1] => Array
(
[0] => name=>Adriana
[1] => firstemail=>add@example.com

)

[2] => Array
(

[0] => name=>Maria
[1] => firstemail=>maria@example.com

)
)

提前感谢!

1 个答案:

答案 0 :(得分:1)

喜欢这个

$finalarray = array();

foreach ($inputarray as $subarray) {
  foreach($subarray as $entry) {
     if (!array_key_exists($entry["name"], $finalarray)) {
        $finalarray[$entry["name"]] = array();
     }
     array_push($finalarray[$entry["name"]], $entry["email"]);
  }
}