如何使用PHP将两个数组的字符串值成对连接起来?
所以我有两个数组如何使用PHP将两个数组的字符串值成对连接起来?,php,arrays,concatenation,Php,Arrays,Concatenation,所以我有两个数组 Array ( [0] => test [1] => test 1 [2] => test 2 [3] => test 3 ) 及 我想把它们组合在一起,得到这样一个数组 Array ( [0] => test test [1] => test 1 test 1 [2] => test 2 test 2 [3] => test 3 test 3 ) 我发现了很多函数,比如array\u merge和array\u combin
Array
(
[0] => test
[1] => test 1
[2] => test 2
[3] => test 3
)
及
我想把它们组合在一起,得到这样一个数组
Array
(
[0] => test test
[1] => test 1 test 1
[2] => test 2 test 2
[3] => test 3 test 3
)
我发现了很多函数,比如array\u merge
和array\u combine
,但是没有任何函数可以完成我想做的事情
有什么想法吗
提前谢谢
Max您可以循环使用它来创建一个新数组。没有内置的功能。欢迎来到精彩的编程世界:) 提示:
- 可以将两个字符串与“”组合
$combined = array();
for($i = 0, $l = min(count($a1), count($a2)); $i < $l; ++$i) {
$combined[$i] = $a1[$i] . $a2[$i];
}
$combined=array();
对于($i=0,$l=min(计数($a1),计数($a2));$i<$l;+$i){
$combled[$i]=$a1[$i]。$a2[$i];
}
根据您的喜好调整循环:仅连接最小数量的元素,如果其中一个数组较短,则连接空字符串,等等。您可以按照自己的喜好进行操作
for($i; $i<count($a); $i++)
{
$arr[$i] = $a[$i]." ".$b[$i];
}
对于($i;$i只需循环并将串联分配给新数组:
$array1=array("test","test 1","test 2","test 3");
$array2=array("x","y","z","w");
$new_array=array();
foreach (range(0,count($array1)-1) as $i)
{
array_push($new_array,$array1[$i] . $array2[$i]);
}
假设这两个数组是$array1和$array2
for($x = 0; $x < count($array2); $x++){
$array1[$x] = $array1[$x] . ' ' . $array2[$x];
}
($x=0;$x{
$array1[$x]=$array1[$x].'。$array2[$x];
}
您可以使用数组\u map
:
$combined = array_map(function($a, $b) { return $a . ' ' . $b; }, $a1, $a2));
如果您使用的是PHP5.3.0+,下面是一个单行解决方案:
$result = array_map(function ($x, $y) { return $x.$y; }, $array1, $array2);
如果数据来自两个不同的查询,并且它们变成了两个不同的数组,那么组合它们并不总是一个答案 当将它们放入数组([])中时,可以使用foreach循环计算数量,然后循环在一起 注意:它们在每个数组中的数量必须相同,否则一个数组可能会在另一个数组之前完成
foreach ($monthlytarget as $value) {
// find how many results there were
$loopnumber++;
}
echo $loopnumber;
for ($i = 0; $i < $loopnumber; $i++) {
echo $shop[$i];
echo " - ";
echo $monthlytarget[$i];
echo "<br>";
}
foreach($monthlytargetas$value){
//找出有多少个结果
$loopnumber++;
}
echo$loopnumber;
对于($i=0;$i<$loopnumber;$i++){
echo$shop[$i];
回声“-”;
echo$monthlytarget[$i];
回声“
”;
}
然后将显示:-
特易购-78Asda-89
莫里森-23
塞恩斯伯里-46
您甚至可以添加计数编号以显示此列表项目编号……许多答案都建议使用
数组映射方式,而对于循环方式,则建议使用更简单的
我认为array\u map
解决方案看起来比在数组上循环并在for
循环中构建串联数组更好,更“高级”,但与我的预期相反,它比常规for
慢得多
我在ubuntu 16.04.1上用PHP版本7.1.23-4运行了一些测试:两个数组,每个数组包含250k个10位随机数的元素,解决方案运行20次需要4.7004秒,而数组映射解决方案运行20次需要11.7939秒,几乎慢了2.5倍
我希望PHP能够更好地优化内置的array\u map
特性,而不是for
循环,但看起来恰恰相反
我测试过的代码:
// Init the test
$total_time_for = 0;
$total_time_arraymap = 0;
$array1 = [];
$array2 = [];
for ( $i = 1; $i <= 250000; $i ++ ) {
$array1[] = mt_rand(1000000000,9999999999);
$array2[] = mt_rand(1000000000,9999999999);
}
// Init completed
for ( $j = 1; $j <= 20; $j ++ ) {
// Init for method
$array_new = [];
$startTime = microtime(true);
// Test for method
for ( $i = 0; $i <= count($array1); $i ++ ) {
$array_new[] = $array1[$i] . " " . $array2[$i];
}
// End of test content
$endTime = microtime(true);
$elapsed = $endTime - $startTime;
$total_time_for += $elapsed;
//echo "for - Execution time : $elapsed seconds" . "\n";
unset($array_new);
//----
// Init array_map method
$array_new = [];
$startTime = microtime(true);
// Test array_map method
$array_new = array_map(function($a, $b) { return $a . ' ' . $b; }, $array1, $array2);
// End of test content
$endTime = microtime(true);
$elapsed = $endTime - $startTime;
$total_time_arraymap += $elapsed;
//echo "array_map - Execution time : $elapsed seconds" . "\n";
unset($array_new);
}
echo "for - Total execution time : $total_time_for seconds" . "\n";
echo "array_map - Total execution time : $total_time_arraymap seconds" . "\n";
// Init the test
$total_time_for = 0;
$total_time_arraymap = 0;
$array1 = [];
$array2 = [];
for ( $i = 1; $i <= 250000; $i ++ ) {
$array1[] = mt_rand(1000000000,9999999999);
$array2[] = mt_rand(1000000000,9999999999);
}
function combine($a, $b) {
return $a . ' ' . $b;
}
// Init completed
for ( $j = 1; $j <= 20; $j ++ ) {
// Init for method
$array_new = [];
$startTime = microtime(true);
// Test for method
for ( $i = 0; $i <= count($array1); $i ++ ) {
$array_new[] = combine($array1[$i], $array2[$i]);
}
// End of test content
$endTime = microtime(true);
$elapsed = $endTime - $startTime;
$total_time_for += $elapsed;
//echo "for external function call - Execution time : $elapsed seconds" . "\n";
unset($array_new);
//----
// Init array_map method
$array_new = [];
$startTime = microtime(true);
// Test array_map method
$array_new = array_map('combine', $array1, $array2);
// End of test content
$endTime = microtime(true);
$elapsed = $endTime - $startTime;
$total_time_arraymap += $elapsed;
//echo "array_map external function call - Execution time : $elapsed seconds" . "\n";
unset($array_new);
}
echo "for external function call - Total execution time : $total_time_for seconds" . "\n";
echo "array_map external function call - Total execution time : $total_time_arraymap seconds" . "\n";
长话短说,总的结论:
- 调用预定义函数:使用
array\u map
,所需时间减少约40%(8.7秒vs.12.8秒)
- 在需要的地方执行数组操作:使用
进行循环,所需时间减少约60%(4.7秒vs.11.8秒)
- 在使用预定义函数或在需要时(重新)实现它之间进行选择:使用
for
循环并在循环内实现所需的操作,所需时间减少约45%(4.7秒vs.8.7秒)
基于此,在您的特定用例中,对
循环使用,并在循环体内部进行串联,而不调用其他函数。是的,我想知道是否有一个函数为我做了这件事^ ^鉴于PHP中有许多内置函数,我认为这不是一个不合理的问题问。一个人可以很容易地浏览它们,而不需要别人帮他做。这是一个很好的回答。不是通常的“为代表写愚蠢的代码”种类。这是一个优雅的解决方案,但是它的性能不如for
循环,甚至不如array\u map
调用预定义的命名函数。请参阅下面的答案进行比较。似乎在匿名函数上应用array\u map
从来都不是一个好主意。它的速度可能比简单的for慢2.5倍
loop。可能是因为引擎为每个元素重新实例化了匿名函数,或者谁知道为什么……在大多数现实世界的用例中,这两种解决方案之间的性能差异是可以忽略的。两种解决方案都很好-使用您找到的任何一种都可以使代码更具可读性。数组映射使用是一种错误的可能性很小如果基准测试显示确实如此,那么切换到for解决方案,但一般来说,您应该避免像这样的微优化,并针对可读性进行优化。
// Init the test
$total_time_for = 0;
$total_time_arraymap = 0;
$array1 = [];
$array2 = [];
for ( $i = 1; $i <= 250000; $i ++ ) {
$array1[] = mt_rand(1000000000,9999999999);
$array2[] = mt_rand(1000000000,9999999999);
}
function combine($a, $b) {
return $a . ' ' . $b;
}
// Init completed
for ( $j = 1; $j <= 20; $j ++ ) {
// Init for method
$array_new = [];
$startTime = microtime(true);
// Test for method
for ( $i = 0; $i <= count($array1); $i ++ ) {
$array_new[] = combine($array1[$i], $array2[$i]);
}
// End of test content
$endTime = microtime(true);
$elapsed = $endTime - $startTime;
$total_time_for += $elapsed;
//echo "for external function call - Execution time : $elapsed seconds" . "\n";
unset($array_new);
//----
// Init array_map method
$array_new = [];
$startTime = microtime(true);
// Test array_map method
$array_new = array_map('combine', $array1, $array2);
// End of test content
$endTime = microtime(true);
$elapsed = $endTime - $startTime;
$total_time_arraymap += $elapsed;
//echo "array_map external function call - Execution time : $elapsed seconds" . "\n";
unset($array_new);
}
echo "for external function call - Total execution time : $total_time_for seconds" . "\n";
echo "array_map external function call - Total execution time : $total_time_arraymap seconds" . "\n";