开发者

Compare 2 arrays that exist in the same multidimensional array

I have an associative array as such:

Array
(
[0] => Array
    (
        [0] => Array
            (
                [0] => nam开发者_StackOverflowe=>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
            )
    )
)

I want to collect all email addresses of all members. Jose has 2 email addresses, Maria 1, and Adriana 1. How do I compare 2 arrays that co-exist in a multidimensional array?

I want something like:

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

)
)

thanks in advance!


Like this

$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"]);
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜