PHP使用for循环合并数组而不使用array_merge函数

时间:2021-05-26 21:14:32

I am learning more about for loops and would like to see how do you merge arrays using only for loops and not using built-in PHP functions such as array_merge().

我正在学习更多有关for循环的知识,并希望看到如何仅使用for循环合并数组,而不使用内置的PHP函数,如array_merge()。

I know you can use foreach to do this, but how would this work using only for loops and not foreach?

我知道你可以使用foreach来做到这一点,但是这怎么会只用于循环而不是foreach呢?

foreach example:

$array1 = ['judas', 'john', 'michael'];

$array2 = ['fernando', 'jsamine', 'sam', 'walter'];

$array3 = ['judas', 'john', 'mike', 'steve'];

foreach ([$array1, $array2, $array3] as $arr) {
    foreach ($arr as $values) {
        ...
    } 
}

4 个解决方案

#1


Yes, you can do this using just for loops.

是的,你可以只使用for循环来做到这一点。

$array1 = ['judas', 'john', 'michael'];    
$array2 = ['fernando', 'jsamine', 'sam', 'walter'];
$array3 = ['judas', 'john', 'mike', 'steve'];

$all_arrays = [$array1, $array2, $array3];
$merged = [];
for ($i = 0; $i < 3; $i++) {
    $arr = $all_arrays[$i];
    $x = count($arr);
    for ($j=0; $j < $x; $j++) { 
        // Using the value as the key in the merged array ensures 
        // that you will end up with distinct values.
       $merged[$arr[$j]] = 1;
    }
}

// You could use array_keys() to do get the final result, but if you
// want to use just loops then it would work like this:
$final = [];
$x = count($merged);
for ($i=0; $i < $x; $i++) { 
  $final[] = key($merged);
  next($merged);
}

var_dump($final);

key() and next() ARE php functions. But I really do not know of a way to get to the keys without using either foreach or some php function.

key()和next()是php函数。但我真的不知道如何在不使用foreach或某些php函数的情况下获取密钥。

#2


Iterate each array on its own:

迭代每个数组:

foreach($array2 as $v)
  if(!in_array($v, $array1))
    $array1[] = $v;
foreach($array3 as $v)
  if(!in_array($v, $array1))
    $array1[] = $v;

Or simply use array_merge() - there is no reason for do not do it.

或者只是使用array_merge() - 没有理由不这样做。

#3


$a=array('1','2','3','4','5');
$b=array('a','b','c','d','e');
$c=count($b);
      for($i=0; $i<$c; $i++) 
          {
             $a[]=$b[$i];  // each element  1 by 1 store inside the array $a[]
          }
print_r($a);  

#4


function arrayMerge(array $arrays) {
    $mergeArray = [];

    foreach ($arrays as  $key => $array) {
        foreach($array as $finalArray) {
            $mergeArray[] = $finalArray;
        }
    }

   return $mergeArray;
}


$array1 = [
    'Laravel', 'Codeigniter', 'Zend'
];

$array2 = [
    'Node js', 'Vue js', 'Angular js'
];

$array3 = [
    'Python', 'Django', 'Ruby'
];

print_r(arrayMerge([$array1, $array2, $array3]));

#1


Yes, you can do this using just for loops.

是的,你可以只使用for循环来做到这一点。

$array1 = ['judas', 'john', 'michael'];    
$array2 = ['fernando', 'jsamine', 'sam', 'walter'];
$array3 = ['judas', 'john', 'mike', 'steve'];

$all_arrays = [$array1, $array2, $array3];
$merged = [];
for ($i = 0; $i < 3; $i++) {
    $arr = $all_arrays[$i];
    $x = count($arr);
    for ($j=0; $j < $x; $j++) { 
        // Using the value as the key in the merged array ensures 
        // that you will end up with distinct values.
       $merged[$arr[$j]] = 1;
    }
}

// You could use array_keys() to do get the final result, but if you
// want to use just loops then it would work like this:
$final = [];
$x = count($merged);
for ($i=0; $i < $x; $i++) { 
  $final[] = key($merged);
  next($merged);
}

var_dump($final);

key() and next() ARE php functions. But I really do not know of a way to get to the keys without using either foreach or some php function.

key()和next()是php函数。但我真的不知道如何在不使用foreach或某些php函数的情况下获取密钥。

#2


Iterate each array on its own:

迭代每个数组:

foreach($array2 as $v)
  if(!in_array($v, $array1))
    $array1[] = $v;
foreach($array3 as $v)
  if(!in_array($v, $array1))
    $array1[] = $v;

Or simply use array_merge() - there is no reason for do not do it.

或者只是使用array_merge() - 没有理由不这样做。

#3


$a=array('1','2','3','4','5');
$b=array('a','b','c','d','e');
$c=count($b);
      for($i=0; $i<$c; $i++) 
          {
             $a[]=$b[$i];  // each element  1 by 1 store inside the array $a[]
          }
print_r($a);  

#4


function arrayMerge(array $arrays) {
    $mergeArray = [];

    foreach ($arrays as  $key => $array) {
        foreach($array as $finalArray) {
            $mergeArray[] = $finalArray;
        }
    }

   return $mergeArray;
}


$array1 = [
    'Laravel', 'Codeigniter', 'Zend'
];

$array2 = [
    'Node js', 'Vue js', 'Angular js'
];

$array3 = [
    'Python', 'Django', 'Ruby'
];

print_r(arrayMerge([$array1, $array2, $array3]));