结合两个(或更多..)在PHP多维数组 [英] combine two (or more..) multi-dimensional arrays in php

查看:107
本文介绍了结合两个(或更多..)在PHP多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我使用不同的参数多次运行的查询。我使用的XML解析器的结果返回给jQuery的。

I have a query that I run several times with different parameters. I am using an xml parser to return the results to jQuery.

我似乎无法找到一种方法,第一个'点'上的结果不覆盖的第一项结合起来。

I can't seem to find a way to combine the results on the first 'node' without overwriting the first entry.

简体code样品:<​​/ P>

Simplified code sample:

$temp1 = returnArray(0);

$temp2 = returnArray(1);

$data = array_merge($temp1, $temp2);

print_r($data);

function returnArray($switch)
{
    if($switch == 0)
    {
        $data['data']['first'] = "something";
    } else {
        $data['data']['second'] = "something ELSE";
    }
    return $data;
}

结果打印出来阵列([数据] =&GT;数组([秒] =&GT;别的东西)) - $ TEMP2被覆盖$ temp1目录。我明白这是array_merge()的默认行为,这是混淆了我。

results in printing out Array ( [data] => Array ( [second] => something ELSE ) ) - $temp2 is overwriting $temp1. I understand this to be the default behavior of array_merge(), which is confusing to me.

我也试图做这样的事情 $数据= $ temp1目录+ $ TEMP2; 或...

I have also tried doing something like $data = $temp1+$temp2; or...

$data = returnArray(0);

$data += returnArray(1);

打印阵列([数据] =&GT;数组([首页] =&GT;的东西))

或...

$data = returnArray(0);

$data .= returnArray(1);
print_r($data);

打印 ArrayArray

我结束了一起黑客攻击这样的:

I ended up hacking this together:

$data['data'] = returnArray(0);
$data['data'] = returnArray(1);

function returnArray($switch){
  if($switch == 0) return Array('first' => 'something');
  else return Array('second' => 'something else');
}

我敢并不是所有的快乐......虽然它会在这种情况下所做的工作,也不会是所有适用于未来的时候更复杂的情况发生。

Which i'm not all that happy with... Although it gets the job done in this case, it won't be all that applicable in the future when a more complex situation occurs.

有没有更好的方式来采取两种关联,多维数组和不覆盖将它们结合起来?

编辑:

我想方法可重用,可扩展的,能够处理2+阵列,2+尺寸。

I'd like the method to be reusable, extensible and be able to handle 2+ arrays with 2+ dimensions.

推荐答案

PHP实际上提供了一个调用的函数的 array_merge_recursive() 这也正是在使用这种类型的场景。从<一个href=\"http://php.net/manual/en/function.array-merge-recursive.php#refsect1-function.array-merge-recursive-description\"相对=nofollow>文档页面:

PHP actually provides a function called array_merge_recursive() which is used exactly in this type of scenario. From the documentation page:

array_merge_recursive() 的合并
  一个或多个阵列的元件
  在一起,使得一个的值是
  附加到previous的端
  一。它返回的结果数组。

array_merge_recursive() merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

如果输入阵列具有相同的
  字符串键,则这些值
  键合并在一起的
  阵列,这是递归完成,
  这样,如果这些值中的一个是
  阵列本身的功能将合并
  它与一个相应的条目
  另一个数组了。然而,如果该
  阵列具有相同的数字键,则
  后面的值将不会覆盖
  原始值,但将被追加。

If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.

示例:

<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>

输出:

Array
(
    [color] => Array
        (
            [favorite] => Array
                (
                    [0] => red
                    [1] => green
                )

            [0] => blue
        )

    [0] => 5
    [1] => 10
)

这篇关于结合两个(或更多..)在PHP多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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