alex
alex

Reputation: 89

Merge array values for same keys inside loop with php

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>';
}

Upvotes: 1

Views: 3458

Answers (6)

kevin
kevin

Reputation: 300

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);
?>

Upvotes: 0

user2182349
user2182349

Reputation: 9782

$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];
        }
    }
}

Upvotes: 3

kamal pal
kamal pal

Reputation: 4207

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.

Upvotes: 0

Sachin I
Sachin I

Reputation: 1508

$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;
  }
}}

Upvotes: 0

wmk
wmk

Reputation: 4626

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);

Upvotes: 0

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

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

Upvotes: 1

Related Questions