PHP合并两个关联数组

时间:2015-07-06 06:55:50

标签: php arrays merge

$ array 1: -

Array
(
    [Test Stock] => Array
        (
            [intStockCount] => 10
        )

    [CARTON 50 X 50 X 50] => Array
        (
            [intStockCount] => 10
        )
)

$数组2: -

Array
(
    [Test Stock] => Array
        (
            [intInvoiceCount] => 20
        )

    [CARTON 50 X 50 X 50] => Array
        (
            [intInvoiceCount] => 30
        )
)

我需要一个新组合在一起而不使用循环

Array
(
    [Test Stock] => Array
        (
            [intStockCount] => 10
            [intInvoiceCount] => 20
        )

    [CARTON 50 X 50 X 50] => Array
        (
            [intStockCount] => 10
            [intInvoiceCount] => 30
        )
)

2 个答案:

答案 0 :(得分:2)

  $array1 = array(
    'Test Stock' => array(
        'intStockCount' => 10,
    ),
    'CARTON 50 X 50 X 50' => array(
        'intStockCount' => 10
    )
    );
    $array2 = array(
    'Test Stock' => array(
        'intStockCount' => 10,
    ),
    'CARTON 50 X 50 X 50' => array(
        'intStockCount' => 10
    )
    );
    $result = array_merge_recursive($array1, $array1);

    var_dump($result);

$array1 = array( 'Test Stock' => array( 'intStockCount' => 10, ), 'CARTON 50 X 50 X 50' => array( 'intStockCount' => 10 ) ); $array2 = array( 'Test Stock' => array( 'intStockCount' => 10, ), 'CARTON 50 X 50 X 50' => array( 'intStockCount' => 10 ) ); $result = array_merge_recursive($array1, $array1); var_dump($result); 你必须改变阵列结构。

对于普通阵列marge:     $ result = array_merge($ array1,$ array2);

http://php.net/manual/en/function.array-merge.php

答案 1 :(得分:2)

您可以使用/** * Uses recursion to perform for-like loop. * * Usage is * * MultiForLoop.loop( new int[]{2,4,5}, new MultiForLoop.Callback() { * void act(int[] indices) { * System.err.printf("%d %d %d\n", indices[0], indices[1], indices[2] ); * } * } * * It only does 0 - (n-1) in each direction, no step or start * options, though they could be added relatively trivially. */ public class MultiForLoop { public static interface Callback { void act(int[] indices); } static void loop(int[] ns, Callback cb) { int[] cur = new int[ns.length]; loop(ns, cb, 0, cur); } private static void loop(int[] ns, Callback cb, int depth, int[] cur) { if(depth==ns.length) { cb.act(cur); return; } for(int j = 0; j<ns[depth] ; ++j ) { cur[depth]=j; loop(ns,cb, depth+1, cur); } } } 来完成这项工作。

array_merge_recursive

  

array_merge_recursive()将一个或多个数组的元素合并在一起,以便将一个值的值附加到前一个数组的末尾。它返回结果数组。

     

如果输入数组具有相同的字符串键,则这些键的值将合并到一个数组中,并且这是递归完成的,因此如果其中一个值是数组本身,则该函数将合并它另一个数组中的相应条目。但是,如果数组具有相同的数字键,则后面的值不会覆盖原始值,但会被追加。

只是做:

array_merge_recursive

这里要注意的重要一点是,您在<?php $array1 = array( "Test Stock" => array( "intStockCount" => 10 ), "CARTON 50 X 50 X 50" => array( "intStockCount" => 10 ) ); $array2 = array( "Test Stock" => array( "intInvoiceCount" => 20 ), "CARTON 50 X 50 X 50" => array( "intInvoiceCount" => 30 ) ); $final = array_merge_recursive($array1,$array2); echo '<pre>'; print_r($final); echo '</pre>'; /* OUTPUT Array ( [Test Stock] => Array ( [intStockCount] => 10 [intInvoiceCount] => 20 ) [CARTON 50 X 50 X 50] => Array ( [intStockCount] => 10 [intInvoiceCount] => 30 ) ) */ 中使用了相同的字符串key。正如PHP文档所述

  

...这些键的值合并为一个数组