1

Going through foreach loop for filter on my site I get array back like this:

Array
(
    [21] => Blau
    [24] => Azul
    [28] => Blue
)

By choosing next filter another array will be created:

Array
(
    [21] => Grün
    [24] => Verde
    [28] => Green
)

and so on...

What I what to do is merge values of these arrays on same keys. So it have to be look like this:

Array
( 
    [21] => Blau-Grün
    [24] => Azul-Verde
    [28] => Blue-Green
)

It worked with Uchiha's code. I made some changed inside my loop:

foreach (...){
    //some logic before 
    $array[] = array();
    $i = 0;
    foreach($array as $k => $v){
        $i++;
        foreach($array[$k] as $key => $value){
            if(array_key_exists($key, $array[$i])){
                $result[$key] = $value . '-' . $array[$i][$key];
            }
        }
    }
    echo '<pre>' . print_r($result,true) . '</pre>';
}
2
  • 1
    Have you tried something ? Commented Jun 10, 2015 at 11:32
  • @Rizier123 Yes, I did. I can't make any of suggestions work in my case. I think my problem is: I can't separate described arrays to different varaibles (like &array1 and $array2 etc). Each of these arrays will be produced while foreach loop and saved in same varaiable. Commented Jun 10, 2015 at 11:58

6 Answers 6

3
$maxItems = max(count($blue),count($green));

for ($i = 0; $i < $maxItems; $i++) {
    if (isset($blue[$i], $green[$i])) {
        $combined[$i] = $blue[$i].'-'.$green[$i];
    } else {
        if (isset($blue[$i])) {
            $combined[$i] = $blue[$i];
        } else {
            $combined[$i] = $green[$i];
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Uchiha - I added the missing right brace.
Even though your output is not meeting OPs output Check This
1

You can use foreach along with array_key_exists as

foreach($arr1 as $key => $value){
    if(array_key_exists($key, $arr2)){
        $result[$key] = $value.'-'.$arr2[$key];
    }
}

Fiddle

Comments

0

you can use array_map with a closure that joins the two values, e.g.

$joined = array_map(function($m, $n){ return $m . '-' . $n; }, $array1, $array2);

1 Comment

Hi @wmk, i checked your solution, but i have noticed that if array2, have more than array1 it is appending(not matching is also adding), but here should not append, can you pls check it out.
0
$foo = array(1 => "Bob", 2 => "Sepi");
$bar = array(1 => "sach", 2 => "john");

$foobar= array();
foreach ($foo as $key => $value) {
  foreach ($bar as $key1 => $value1) {
   if($key == $key1){
  $foobar[] = $value."-".$value1;
  }
}}

Comments

0

You can create a function, which takes both of arrays as parameter and identify which one has more elements, loop through and concat the string from another array at the same key.

$array1 = array(
    'Blau',
    'Azul',
    'Blue'
);

$array2 = array(
    'Grün',
    'Verde',
    'Green'
)

function mergeArray($array1, $array2){
    $newArray = array();
    if(count($array1) >= count($array2)){
        foreach($array1 as $key => $value){
                $newArray[] = $value .  (array_key_exists($key, $array2) ? '-' . $array2[$key] : '');
        }
    } else {
        foreach($array2 as $key => $value){
                $newArray[] = $value .  (array_key_exists($key, $array1) ? '-' . $array1[$key] : '');
        }
    }
}
$newArray = mergeArray($array1, $array2);

$newArray contains the result you expected.

1 Comment

This will not work. Nothing garantee that both arrays have the same number of values, also putting a count() in a loop is not recommended as it slows down the loop. if array 1 is bigger than array 2 your code will break.
0

here is simple and tested code

<?php
$a1=array(
    1 => 'Blau',
    2 => 'Azul',
    3 => 'Blue'
);
$a2=array
(
    1 => 'Grün',
    2 => 'Verde',
    4 => 'Green'
);
$a3=$a1 + $a2; 
$a4=$a2 + $a1;

foreach($a3 as $key=>$val){
    if($a4[$key] != $a3[$key]){
        $a3[$key] = $a3[$key].' '.$a4[$key];
    }
}
print_r($a3);
?>

Comments

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.