1

How I can merge n arrays in PHP. The number of arrays is determined by the number of users who want to extract some information.

I need to make something like this

$result = array_merge_recursive ($tweets0, $tweets1, $tweets2, $tweets3, $tweets4, $tweets5, ... n);

How could I solve it?

An example with 3 arrays

$tweets0
Array
(
    [statuses] => Array
        (
        )

    [search_metadata] => Array
        (
            [completed_in] => 0.021
            [max_id] => 4.2466715126216E+17
            [max_id_str] => 424667151262158848
            [query] => from%3A%40nopiedra+AND+%23utpl
            [refresh_url] => ?since_id=424667151262158848&q=from%3A%40nopiedra%20AND%20%23utpl&result_type=recent
            [count] => 50
            [since_id] => 0
            [since_id_str] => 0
        )

)

$tweets1
Array
(
    [statuses] => Array
        (
            [0] => Array
                (
                    [metadata] => Array
                        (
                            [result_type] => recent
                            [iso_language_code] => es
                        )

                    [created_at] => Fri Jan 17 18:07:56 +0000 2014
                    [id] => 4.2424169391194E+17
                    [id_str] => 424241693911941120
                    [text] => Alguna alma caritativa de la #utpl podría ayudarme con las pregunta de Expresión oral que bestia ni contabilidad es tan complicada como esta
                    [source] => Twitter for Android
                    [truncated] => 
                    [in_reply_to_status_id] => 
                    [in_reply_to_status_id_str] => 
                    [in_reply_to_user_id] => 
                    [in_reply_to_user_id_str] => 
                    [in_reply_to_screen_name] => 
                    [user] => Array
                        (
                            [id] => 99147465
                            [id_str] => 99147465
                            [name] =>  Steve Cedeño
                            [screen_name] => stevecede
                            [location] => 
                            [description] => Barcelonista ,gusto del rock y en contra del regueton,adicto al Playstation 3... since 1982
                            [url] => https://t.co/Miwfx1QAu4
                        )
                )

        )

    [search_metadata] => Array
        (
            [completed_in] => 0.015
            [max_id] => 4.2424169391194E+17
            [max_id_str] => 424241693911941120
            [query] => from%3A%40stevecede+AND+%23utpl
            [refresh_url] => ?since_id=424241693911941120&q=from%3A%40stevecede%20AND%20%23utpl&result_type=recent
            [count] => 50
            [since_id] => 0
            [since_id_str] => 0
        )

)

$tweets2
Array
(
    [statuses] => Array
        (
            [0] => Array
                (
                    [metadata] => Array
                        (
                            [result_type] => recent
                            [iso_language_code] => es
                        )

                    [created_at] => Sat Jan 18 16:14:56 +0000 2014
                    [id] => 4.2457564273929E+17
                    [id_str] => 424575642739290112
                    [text] => Hall del edificio #UGTI de la #UTPL congestionadísimo transite con cuidado... XD
                    [source] => TweetDeck
                    [truncated] => 
                    [in_reply_to_status_id] => 
                    [in_reply_to_status_id_str] => 
                    [in_reply_to_user_id] => 
                    [in_reply_to_user_id_str] => 
                    [in_reply_to_screen_name] => 
                    [user] => Array
                        (
                            [id] => 14938800
                            [id_str] => 14938800
                            [name] => Xavier Jaramillo 
                            [screen_name] => xavito90
                            [location] => Loja
                            [description] => Estudiante de Ingeniería en Sistemas... Del bombillo soy...!!!
                            [url] => http://t.co/SestnLJ58A
                        )
                )

        )

    [search_metadata] => Array
        (
            [completed_in] => 0.017
            [max_id] => 4.2457564273929E+17
            [max_id_str] => 424575642739290112
            [query] => from%3A%40xavito90+AND+%23utpl
            [refresh_url] => ?since_id=424575642739290112&q=from%3A%40xavito90%20AND%20%23utpl&result_type=recent
            [count] => 50
            [since_id] => 0
            [since_id_str] => 0
        )
)

The result is

Array
(
    [statuses] => Array
        (
            [0] => Array
                (
                    [metadata] => Array
                        (
                            [result_type] => recent
                            [iso_language_code] => es
                        )

                    [created_at] => Fri Jan 17 18:07:56 +0000 2014
                    [id] => 4.2424169391194E+17
                    [id_str] => 424241693911941120
                    [text] => Alguna alma caritativa de la #utpl podría ayudarme con las pregunta de Expresión oral que bestia ni contabilidad es tan complicada como esta
                    [source] => Twitter for Android
                    [truncated] => 
                    [in_reply_to_status_id] => 
                    [in_reply_to_status_id_str] => 
                    [in_reply_to_user_id] => 
                    [in_reply_to_user_id_str] => 
                    [in_reply_to_screen_name] => 
                    [user] => Array
                        (
                            [id] => 99147465
                            [id_str] => 99147465
                            [name] =>  Steve Cedeño
                            [screen_name] => stevecede
                            [location] => 
                            [description] => Barcelonista ,gusto del rock y en contra del regueton,adicto al Playstation 3... since 1982
                            [url] => https://t.co/Miwfx1QAu4
                        )
                )

            [1] => Array
                (
                    [metadata] => Array
                        (
                            [result_type] => recent
                            [iso_language_code] => es
                        )

                    [created_at] => Sat Jan 18 16:14:56 +0000 2014
                    [id] => 4.2457564273929E+17
                    [id_str] => 424575642739290112
                    [text] => Hall del edificio #UGTI de la #UTPL congestionadísimo transite con cuidado... XD
                    [source] => TweetDeck
                    [truncated] => 
                    [in_reply_to_status_id] => 
                    [in_reply_to_status_id_str] => 
                    [in_reply_to_user_id] => 
                    [in_reply_to_user_id_str] => 
                    [in_reply_to_screen_name] => 
                    [user] => Array
                        (
                            [id] => 14938800
                            [id_str] => 14938800
                            [name] => Xavier Jaramillo 
                            [screen_name] => xavito90
                            [location] => Loja
                            [description] => Estudiante de Ingeniería en Sistemas... Del bombillo soy...!!!
                            [url] => http://t.co/SestnLJ58A
                        )
                )

        )

    [search_metadata] => Array
        (
            [completed_in] => Array
                (
                    [0] => 0.021
                    [1] => 0.015
                    [2] => 0.017
                )

            [max_id] => Array
                (
                    [0] => 4.2466715126216E+17
                    [1] => 4.2424169391194E+17
                    [2] => 4.2457564273929E+17
                )

            [max_id_str] => Array
                (
                    [0] => 424667151262158848
                    [1] => 424241693911941120
                    [2] => 424575642739290112
                )

            [query] => Array
                (
                    [0] => from%3A%40nopiedra+AND+%23utpl
                    [1] => from%3A%40stevecede+AND+%23utpl
                    [2] => from%3A%40xavito90+AND+%23utpl
                )

            [refresh_url] => Array
                (
                    [0] => ?since_id=424667151262158848&q=from%3A%40nopiedra%20AND%20%23utpl&result_type=recent
                    [1] => ?since_id=424241693911941120&q=from%3A%40stevecede%20AND%20%23utpl&result_type=recent
                    [2] => ?since_id=424575642739290112&q=from%3A%40xavito90%20AND%20%23utpl&result_type=recent
                )

            [count] => Array
                (
                    [0] => 50
                    [1] => 50
                    [2] => 50
                )

            [since_id] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                )

            [since_id_str] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                )

        )

)

This result is achievement with array_merge_recursive($tweets0, $tweets1, $tweets2) But what can I do when I join more arrays, and do not know whose number

I appreciate your help

0

4 Answers 4

2

If you have array of N arrays, do it:

 $tweets = array($tweets0, $tweets1, $tweets2, $tweets3, $tweets4, $tweets5, ... n);

 $merged_tweets =  call_user_func_array('array_merge_recursive',$tweets);

See call_user_func_array.

Sign up to request clarification or add additional context in comments.

Comments

1

Your safest bet is to store all the tweets into one array, instead of multiple variables.

And then something like:

$combined_tweets = array();
foreach($tweets as $k => $v)
    $combined_tweets = array_merge_recursive($combined_tweets, $v);

Comments

0

Try to use foreach

$arrs = array($tweets0, $tweets1, $tweets2, $tweets3, $tweets4, $tweets5);

$result = array();
foreach ($arrs as $arr) {
    $result = array_merge_recursive ($result, $arr);
}
var_dump($result);

Comments

0

Solution:

      Use the php function array_merge().

Syntax:

       array array_merge ( array $array1 [, array $... ] );

Description:

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

Example:

Code:     
   <?php

     $array1 = array("color" => "red", 2, 4);
     $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
     $result = array_merge($array1, $array2);
     print_r($result);

   ?>

Output:

  Array
     (
      [color] => green
          [0] => 2
          [1] => 4
          [2] => a
          [3] => b
      [shape] => trapezoid
          [4] => 4
    )

Hope it will work.

1 Comment

And hе needs to use the array_merge_recursive() function, not the array_merge().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.