如何在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)
的成就但是当我加入更多数组时我该怎么办,而且不知道谁的数字
感谢您的帮助
答案 0 :(得分:2)
如果您有N个数组的数组,请执行以下操作:
$tweets = array($tweets0, $tweets1, $tweets2, $tweets3, $tweets4, $tweets5, ... n);
$merged_tweets = call_user_func_array('array_merge_recursive',$tweets);
答案 1 :(得分:1)
您最安全的选择是将所有推文存储到一个数组中,而不是存储多个变量。
然后像:
$combined_tweets = array();
foreach($tweets as $k => $v)
$combined_tweets = array_merge_recursive($combined_tweets, $v);
答案 2 :(得分:0)
尝试使用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 :(得分:0)
<强>解决方案:强>
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
)
希望它能奏效。