在JSON上组合来自foreach的数组

时间:2012-07-29 15:51:54

标签: php arrays json

$data = json_decode($data, true);

这个$ data变量的一个php foreach循环

foreach($data as $names) {
  echo '<pre>';
  print_r($names);
  echo '</pre>';
}

带回两个多维数组,类似于我在下面制作的这个例子

Array
(
 [0] => Array
    (
        [@attributes] => Array
            (
                [name] => responseHeader
            )

        [int] => Array
            (
                [0] => 0
                [1] => 1
            )

        [lst] => Array
            (
                [@attributes] => Array
                    (
                        [name] => params
                    )

                [str] => Array
                    (
                        [0] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [name] => explainOther
                                    )

                            )

                        [1] => 0
                        [2] => on
                        [3] => content
                        [4] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [name] => wt
                                    )

                            )

                        [5] => on
                        [6] => 2.2
                        [7] => 1000
                        [8] => *,score
                        [9] => on
                        [10] => 0
                        [11] => content
                        [12] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [name] => fq
                                    )

                            )

                    )

            )

    )

[1] => Array
    (
        [@attributes] => Array
            (
                [name] => highlighting
            )

        [lst] => Array
            (
                [@attributes] => Array
                    (
                        [name] => 18900
                    )

                [arr] => Array
                    (
                        [@attributes] => Array
                            (
                                [name] => content
                            )

                        [str] => A paragraph of text right here number 1.
                    )



    )

)



Array
(
[@attributes] => Array
    (
        [name] => response
        [numFound] => 1
        [start] => 0
        [maxScore] => 0.4654925
    )

[doc] => Array
    (
        [float] => 0.4654925
        [str] => Array
            (
                [0] => nal
                [1] => another paragraph text 4
                [2] => 18900
                [3] => ma
                [4] => ran
                [5] => 5
                [6] => 18
            )

    )

我想将它们组合成一个数组,我怎样才能使用php,谢谢

2 个答案:

答案 0 :(得分:0)

你见过array_merge吗?

$output = array();
foreach($data as $names) {
    $output = array_merge($output, $names);
}

答案 1 :(得分:0)

使用array_merge() php函数。使用(array) $array_from_json_decode进行类型转换,因为有时您最终会使用stdClass而不是数组。