Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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) {
        ...
    } 
}
share|improve this question
    
your foreach is impossible. – A-2-A May 8 '15 at 22:17
    
@anantkumarsingh why? – Don't Panic May 8 '15 at 22:54
    
foreach didn't work in this manner. – A-2-A May 8 '15 at 23:02
up vote 1 down vote accepted

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.

share|improve this answer
    
I'm looking to do this with for loops not foreach. – cattywampus May 8 '15 at 22:27
    
Oops, I totally missed that part of the question. :( It's still possible. I will edit. – Don't Panic May 8 '15 at 22:30
    
@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. – Don't Panic May 8 '15 at 22:50
    
Just trying to get an understanding on how to use for loops in different situations. In other words for practice. – cattywampus May 8 '15 at 22:57
    
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. – Don't Panic May 8 '15 at 23:01

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.

share|improve this answer
    
Can this be doing using for loops? Not foreach – cattywampus May 8 '15 at 22:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.