两个数组的串联值[重复]

时间:2021-10-25 11:41:50

This question already has an answer here:

这个问题已经有了答案:

Let's assume I have an two arrays and I want to merge every value with the other value of the array.

假设我有两个数组,我想要将每个值与数组的其他值合并。

Array 1

数组1

array (size=2)
  0 => 1
  1 => 2

Array 2

数组2

array (size=2)
  0 => 3
  1 => 4

Wanted result array / string:

想要的结果数组/字符串:

array (size=4)
  0 => '1,3'
  1 => '1,4'
  2 => '2,3'
  3 => '2,4'

I can't get my head around it. Obviously I would need to merge every one array key/value with the other ones. Is there a more elegant way then doing this in a while/foreach loop?

我想不起来了。显然,我需要将每个数组键/值与其他数组合并。是否有一种更优雅的方法在稍后/foreach循环中执行?

3 个解决方案

#1


2  

You need a foreach loop inside a foreach loop. (Actualy, you will have to loop through both arrays to get a concatenated product of both arrays, you don't actually need two foreach loops). You could mix: whiles, foreach, for, or php filter/intersect array functions

在foreach循环中需要一个foreach循环。(实际上,您需要遍历两个数组才能得到两个数组的连接结果,实际上不需要两个foreach循环)。您可以混合使用:whiles、foreach、for或php过滤器/intersect函数

Example

$array1 = array(1,2);
$array2 = array(3,4);
$result = array();

foreach ($array1 as $item1){
   foreach($array2 as $item2){
      $result[] = $item1.','.$item2;
   }
}

https://eval.in/215001

https://eval.in/215001

your result array Length will be array1.Length * array2.Length

结果数组长度为array1。长度* array2.Length

2d arrays

You could also put an array inside an array like this:

你也可以把一个数组放在这样的数组中:

$array1 = array(1,2);
$array2 = array(3,4);
$result = array();

foreach ($array1 as $item1){
   foreach($array2 as $item2){
      $result[] = array($item1, $item2);
   }
}
//$result[0][0] = 1 -- $result[0][1] = 3
//$result[1][0] = 1 -- $result[1][1] = 4
//$result[2][0] = 2 -- $result[2][1] = 3
//$result[3][0] = 2 -- $result[3][1] = 4

We call this a 2d (2 dimensional) array, because you could grapicly display this as a grid, like displayed here above. If you would put an Array, inside an Array inside an Array, you would call this a 3 dimensional array, etc.

我们称它为二维(二维)数组,因为你可以用图形方式将它显示为一个网格,如上图所示。如果你在数组里放一个数组,你会把它叫做三维数组,等等。

print_r($result); in php:

print_r(结果);在php中:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 3
        )

    [1] => Array
        (
            [0] => 1
            [1] => 4
        )

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

    [3] => Array
        (
            [0] => 2
            [1] => 4
        )

)

#2


1  

try

试一试

$a= array ('0' => 1,'1' => 2);
$b= array ('0' => 3,'1' => 4);
for($i=0; $i<count($a); $i++) {
  for($j=0; $j<count($b); $j++) {
    $newarr[]= $a[$i].','.$b[$j];
  }
}
print_r($newarr);//Array ( [0] => 1,3 [1] => 1,4 [2] => 2,3 [3] => 2,4 ) 

#3


1  

$a=array('1','2');
$b=array('3','4');
$res=array();
for($i=0;$i<count($a);$i++)
{
    foreach($b as $bb)
    {
        $res[]=strval($a[$i].','.$bb);
    }


}
print_r($res);//output=Array ( [0] => 1,3 [1] => 1,4 [2] => 2,3 [3] => 2,4 ) 

#1


2  

You need a foreach loop inside a foreach loop. (Actualy, you will have to loop through both arrays to get a concatenated product of both arrays, you don't actually need two foreach loops). You could mix: whiles, foreach, for, or php filter/intersect array functions

在foreach循环中需要一个foreach循环。(实际上,您需要遍历两个数组才能得到两个数组的连接结果,实际上不需要两个foreach循环)。您可以混合使用:whiles、foreach、for或php过滤器/intersect函数

Example

$array1 = array(1,2);
$array2 = array(3,4);
$result = array();

foreach ($array1 as $item1){
   foreach($array2 as $item2){
      $result[] = $item1.','.$item2;
   }
}

https://eval.in/215001

https://eval.in/215001

your result array Length will be array1.Length * array2.Length

结果数组长度为array1。长度* array2.Length

2d arrays

You could also put an array inside an array like this:

你也可以把一个数组放在这样的数组中:

$array1 = array(1,2);
$array2 = array(3,4);
$result = array();

foreach ($array1 as $item1){
   foreach($array2 as $item2){
      $result[] = array($item1, $item2);
   }
}
//$result[0][0] = 1 -- $result[0][1] = 3
//$result[1][0] = 1 -- $result[1][1] = 4
//$result[2][0] = 2 -- $result[2][1] = 3
//$result[3][0] = 2 -- $result[3][1] = 4

We call this a 2d (2 dimensional) array, because you could grapicly display this as a grid, like displayed here above. If you would put an Array, inside an Array inside an Array, you would call this a 3 dimensional array, etc.

我们称它为二维(二维)数组,因为你可以用图形方式将它显示为一个网格,如上图所示。如果你在数组里放一个数组,你会把它叫做三维数组,等等。

print_r($result); in php:

print_r(结果);在php中:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 3
        )

    [1] => Array
        (
            [0] => 1
            [1] => 4
        )

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

    [3] => Array
        (
            [0] => 2
            [1] => 4
        )

)

#2


1  

try

试一试

$a= array ('0' => 1,'1' => 2);
$b= array ('0' => 3,'1' => 4);
for($i=0; $i<count($a); $i++) {
  for($j=0; $j<count($b); $j++) {
    $newarr[]= $a[$i].','.$b[$j];
  }
}
print_r($newarr);//Array ( [0] => 1,3 [1] => 1,4 [2] => 2,3 [3] => 2,4 ) 

#3


1  

$a=array('1','2');
$b=array('3','4');
$res=array();
for($i=0;$i<count($a);$i++)
{
    foreach($b as $bb)
    {
        $res[]=strval($a[$i].','.$bb);
    }


}
print_r($res);//output=Array ( [0] => 1,3 [1] => 1,4 [2] => 2,3 [3] => 2,4 )