1

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().

I know you can use foreach to do this, but how would this work using only for loops and not 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) {
        ...
    } 
}
3
  • your foreach is impossible. Commented May 8, 2015 at 22:17
  • 1
    @anantkumarsingh why? Commented May 8, 2015 at 22:54
  • foreach didn't work in this manner. Commented May 8, 2015 at 23:02

5 Answers 5

1

Yes, you can do this using just for loops.

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

Sign up to request clarification or add additional context in comments.

8 Comments

I'm looking to do this with for loops not foreach.
Oops, I totally missed that part of the question. :( It's still possible. I will edit.
@cattywampus for loops. I saw another question from you the other day that was similar to this. I'm curious what your interest is in using for loops rather than foreach.
Just trying to get an understanding on how to use for loops in different situations. In other words for practice.
That's cool. I was just wondering. You probably know this, but usually a for loop is used in cases when you have a set number of times you need to do something. Using them to iterate arrays is generally going to be more cumbersome than foreach, especially if you need to access the keys.
|
1
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]));

Comments

0

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.

1 Comment

Can this be doing using for loops? Not foreach
0
$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);  

2 Comments

You need to pay attention to your code format in order for people to read easily your code.
The sample data in this under-explained answer does not resemble the asker's sample data / scenario.
0
$arr1 = array(1,2,3,4,5);
$arr2 = array(2,5,6,7,8);
$allval = array();
foreach($arr2 as $key=>$val){
    $arr1[] = $val;
}
foreach($arr1 as $k=>$v){
    if(!in_array($v,$allval)){
        $allval[] = $v;
    }
}
echo "<pre>"; print_R($allval);

1 Comment

The sample data in this unexplained answer does not resemble the asker's sample data / scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.