简体   繁体   English

根据键合并两个数组

[英]Merge two arrays based on key

UPDATED after trying the below suggestion it now gives the correct affect but enters it to many times. 尝试以下建议后进行了更新,现在可以产生正确的效果,但是可以多次输入。

Hi I have the following code what this does is creates two arrays then blend them into one: 嗨,我有以下代码,它的作用是创建两个数组,然后将它们混合为一个:

$list_claimed_users = get_users('meta_key=claimed'); $ list_claimed_users = get_users('meta_key =已声明');

foreach ( $list_claimed_users  as $list_claimed_user ) {
   $a = get_user_meta($list_claimed_user->ID , "claimed");
   $nicknames[] = get_user_meta($list_claimed_user->ID , "nickname");
   $unserialized[] = unserialize($a[0]);

   foreach($unserialized as $key => &$metadata) array_push($metadata, $nicknames[$key]);

}
$claimed_array[] = $unserialized;

print_r($claimed_array);

BUt it now loops and adds the $a data in an extra time for each loop 现在让它循环,并在每个循环的额外时间中添加$ a数据

 Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 31.08.2016
                    [1] => prize8
                    [2] => Array
                        (
                            [0] => test5
                        )

                    [3] => Array
                        (
                            [0] => test5
                        )

                    [4] => Array
                        (
                            [0] => test5
                        )

                    [5] => Array
                        (
                            [0] => test5
                        )

                )

            [1] => Array
                (
                    [0] => 31.07.2017
                    [1] => prize7
                    [2] => Array
                        (
                            [0] => test6
                        )

                    [3] => Array
                        (
                            [0] => test6
                        )

                    [4] => Array
                        (
                            [0] => test6
                        )

                )

            [2] => Array
                (
                    [0] => 31.08.2017
                    [1] => prize5
                    [2] => Array
                        (
                            [0] => test7
                        )

                    [3] => Array
                        (
                            [0] => test7
                        )

                )

            [3] => Array
                (
                    [0] => 21.08.2017
                    [1] => prize6
                    [2] => Array
                        (
                            [0] => test8
                        )

                )

        )

)

Could anyone please advise the best way to merge the two if if there is a simplier way to achieve this when creating the arrays originally. 如果在最初创建数组时有更简单的方法来实现这一目标,那么任何人都可以建议将两者合并的最佳方法。

Any help would be hugely appreciated 任何帮助将不胜感激

After retrieving the nicknames and the meta-data, but before adding result to $claimed_array , you can do: 在获取昵称和元数据之后,但在将结果添加到$claimed_array ,您可以执行以下操作:

foreach($a as $key => &$metadata) array_push($metadata, $nicknames[$key]);

Then just add $a to the $claimed_array : 然后只需将$a添加到$claimed_array

$claimed_array[] = $a;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM