根据键合并两个数组

时间:2017-08-31 17:45:03

标签: php arrays sorting multidimensional-array

在尝试以下建议后更新它现在给出正确的效果,但多次输入。

您好我有以下代码,它的作用是创建两个数组,然后将它们合并为一个:

$ 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);

现在将其置位循环并在每个循环的额外时间内添加$ 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
                        )

                )

        )

)

如果在最初创建数组时有更简单的方法来实现这一点,有人可以建议合并两者的最佳方法。

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

检索昵称和元数据后,但在将结果添加到$claimed_array之前,您可以执行以下操作:

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

然后只需将$a添加到$claimed_array

$claimed_array[] = $a;