合并两个数组以创建JSON表

时间:2014-02-12 10:05:47

标签: php arrays json charts

我想合并两个数组,以便为​​谷歌图表创建DataTable。我创建了以下两个数组;

Array ( [0] => Array ( [c] => Array ( [0] => Array ( [v] => Non-Indigenous ) 
                                      [1] => Array ( [v] => 5 ) ) ) 
        [1] => Array ( [c] => Array ( [0] => Array ( [v] => Other ) 
                                      [1] => Array ( [v] => 1 ) ) ) ) 

我的第二个阵列是;

Array ( [0] => Array ( [c] => Array ( [0] => Array ( [v] => Aboriginal Significance ) ) )         [1] => Array ( [c] => Array ( [0] => Array ( [v] => Non-Indigenous ) ) ) 
        [2] => Array ( [c] => Array ( [0] => Array ( [v] => Other ) ) ) ) 

现在我想知道如何在PHP中构造以下数组;

Array ( [0] => Array ( [c] => Array ( [0] => Array ( [v] => Aboriginal Significance )                                         [1] => Array ( [v] => 0 )) )   
        [1] => Array ( [c] => Array ( [0] => Array ( [v] => Non-Indigenous ) 
                                      [1] => Array ( [v] => 5 ) ) ) 
        [2] => Array ( [c] => Array ( [0] => Array ( [v] => Other ) 
                                      [1] => Array ( [v] => 1 ) ) ) ) 

任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

如果要覆盖数组,请尝试使用array_replace

   <?php
    $arr1 = array( 
            0 => array( 
                    'c' => array( 
                        0 => array( 'v' => 'Non-Indigenous' ), 
                        1 => array( 'v' => 5 ) 
                    ) 
                ),
            1 => array( 
                    'c' => array( 
                            0 => array( 'v' => 'Other' ),
                            1 => array( 'v' => 1 ) 
                        )    
                )
        );


    $arr2 = array( 
                0 => array( 'c' => array( 
                        0 => array( 'v' => 'Aboriginal Significance' )
                    ) 
                ),         
                1 => array( 'c' => array( 
                    0 => array( 'v' => 'Non-Indigenous' )
                    ) 
                ),
                2 => array( 'c' => array(
                    0 => array( 'v' => 'Other' ) 
                    ) 
                ) 
            ); 

    $result = array_replace($arr2, $arr1);
    print_r($result);
   ?>

答案 1 :(得分:0)

这会对你有帮助,

$array1 = "Your first array";
$array2 = "Your second array";
array_unique(array_merge($array1,$array2), SORT_REGULAR);

array_unique