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

a php foreach loop on this $data variable

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

brings back two multi-dimensional arrays similar to this example I have produced below

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
            )

    )

)

I want to combine them in to one array, how can I do this using php, thanks

Jay
  • 3
  • 2
  • possible duplicate of [PHP: Merge 2 Multidimensional Arrays](http://stackoverflow.com/questions/1558291/php-merge-2-multidimensional-arrays). The problem might be slightly different but I still think the answer there could be helpful... – Lix Jul 29 '12 at 15:55

2 Answers2

0

Have you seen array_merge?

$output = array();
foreach($data as $names) {
    $output = array_merge($output, $names);
}
williamvicary
  • 805
  • 5
  • 20
0

Use array_merge() php function. Type-cast with (array) $array_from_json_decode because sometimes you end up with stdClass instead of array.

Silviu-Marian
  • 10,565
  • 6
  • 50
  • 72