【问题标题】:PHP - How to merge n number of array in phpPHP - 如何在 php 中合并 n 个数组
【发布时间】:2014-02-08 06:03:16
【问题描述】:

如何在 PHP 中合并 n 个数组。数组的数量由想要提取一些信息的用户数量决定。

我需要做这样的事情

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

我该如何解决?

3 个数组的示例

$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
        )
)

结果是

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
                )

        )

)

这个结果是用array_merge_recursive($tweets0, $tweets1, $tweets2)实现的,但是当我加入更多的数组时,我该怎么办,不知道是谁的号码

感谢您的帮助

【问题讨论】:

    标签: php arrays recursion merge


    【解决方案1】:

    解决方案:

          Use the php function array_merge().
    

    语法:

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

    说明:

    将一个或多个数组的元素合并在一起,以便将一个数组的值附加到前一个数组的末尾。它返回结果数组。

    如果输入数组具有相同的字符串键,则该键的后一个值将覆盖前一个。但是,如果数组包含数字键,则后面的值不会覆盖原始值,而是会被追加。

    输入数组中带有数字键的值将在结果数组中使用从零开始的递增键重新编号。

    示例:

    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);
    
       ?>
    

    输出:

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

    希望它会起作用。

    【讨论】:

    • 他需要使用array_merge_recursive()函数,而不是array_merge()
    【解决方案2】:

    尝试使用foreach

    $arrs = array($tweets0, $tweets1, $tweets2, $tweets3, $tweets4, $tweets5);
    
    $result = array();
    foreach ($arrs as $arr) {
        $result = array_merge_recursive ($result, $arr);
    }
    var_dump($result);
    

    【讨论】:

      【解决方案3】:

      您最安全的选择是将所有推文存储到一个数组中,而不是多个变量。

      然后是这样的:

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

      【讨论】:

        【解决方案4】:

        如果你有 N 个数组,就这样做:

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

        call_user_func_array

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-23
          • 2011-02-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多