0
$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 做到这一点,谢谢

4

2 回答 2

0

你见过array_merge吗?

$output = array();
foreach($data as $names) {
    $output = array_merge($output, $names);
}
于 2012-07-29T15:57:48.670 回答
0

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

于 2012-07-29T15:58:09.327 回答