0
$data = json_decode($data, true);

この $data 変数の php foreach ループ

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

以下で作成したこの例に似た2つの多次元配列を返します

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
            )

    )

)

それらを1つの配列に結合したいのですが、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 に答える