合并或联合数组这样吗?在PHP [英] Merge or combine an array like this? on PHP

查看:117
本文介绍了合并或联合数组这样吗?在PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种多维数组PHP(的下面的)。

I have this multi-dimensional PHP array (below).


Array
(
    [0] => Array
        (
            [2] => one@example.com
        )

    [1] => Array
        (
            [4] => two@example.com
        )

    [2] => Array
        (
            [3908] => twenty@example.com
        )

    [3] => Array
        (
            [2548] => eleven@example.com
        )

    [4] => Array
        (
            [3099] => ten@example.com
        )

    [5] => Array
        (
            [5283] => six@example.com
        )

)



我不知道我怎么会合并?或合并?或者干脆不喜欢这样使用PHP(的下面的)。

I was wondering how could I merge? or combine? or simply do it like this using PHP (below).


Array
(
    [2] => one@example.com
    [4] => two@example.com
    [3908] => twenty@example.com
    [2548] => eleven@example.com
    [3099]  => ten@example.com
    [5283]  => six@example.com
)

帮助

推荐答案

您可以在推广通过的 call_user_func_array()。虽然array_merge()重新索引阵列, array_replace()没有,但保留了原有按键,使这是一个单行:

You can "promote" the second level elements to first level elements via call_user_func_array(). And while array_merge() re-indexes the array, array_replace() does not but retains the original keys which makes this a one-liner:

$a = call_user_func_array('array_replace', $a);

测试:

<?php
$a = array (
  array (2 => 'one@example.com'),
  array (4 => 'two@example.com'),
  array (3908 => 'twenty@example.com'),
  array (2548 => 'eleven@example.com'),
  array (3099 => 'ten@example.com'),
  array (5283 => 'six@example.com'),
);
$a = call_user_func_array('array_replace', $a);
var_dump($a);

这篇关于合并或联合数组这样吗?在PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆