简体   繁体   中英

PHP combine two arrays with different keys that have the same value?

I'm trying to come up with a way combine two arrays that have different key names with identical values? The order of that the matching values may differ, so it would need to check if it's a match and then merge if yes. Is there an easy way to accomplish? Any help or guidance is appreciated.

Array 1

Array
(
[0] => Array
    (
        [location_id] => 1
        [location_title] => Centralia
        [location_slug] => Centralia
        [state_name] => Illinois
    )

[1] => Array
    (
        [location_id] => 3
        [location_title] => Columbia
        [location_slug] => columbia
        [state_name] => Illinois
    )

[2] => Array
    (
        [location_id] => 15
        [location_title] => Dixon Hennepin
        [location_slug] => dixon-hennepin
        [state_name] => Illinois
    )
)

Array 2

Array
(
[0] => Array
    (
        [hours_locationid] => 3
    )

[1] => Array
    (
        [hours_locationid] => 1
    )

[2] => Array
    (
        [hours_locationid] => 15
    )
)

Results

Array
(
[0] => Array
    (
        [location_id] => 1
        [location_title] => Centralia
        [location_slug] => Centralia
        [state_name] => Illinois
        [hours_locationid] => 1
    )

[1] => Array
    (
        [location_id] => 3
        [location_title] => Columbia
        [location_slug] => columbia
        [state_name] => Illinois
        [hours_locationid] => 3
    )

[2] => Array
    (
        [location_id] => 15
        [location_title] => Dixon Hennepin
        [location_slug] => dixon-hennepin
        [state_name] => Illinois
        [hours_locationid] => 15
    )
)
foreach($array1 as $i => $data) {
  if(array_search(array('hours_locationid'=>$data['location_id']), $array2) !=== false)
     $array1[$i]['hours_locationid'] = $data['location_id'];
}
foreach ($array1 as $val1) {

    foreach ($array2 as $key => $val2) {

        if ($val1['location_id'] == $val2['hours_locationid']) {

            $val1['hours_locationid'] = $val2['hours_locationid'];
            $results[] = $val1;
            unset($array2[$key]);
        }
    }
}

var_dump($results);

Should work, though as mentioned in the comments this is pretty trivial

Try this below,

replace $array1 and $array2 -> demo : http://codepad.org/1inDuoVe

<?php
$array1 = array(array("a"=>"aaa","b"=>"bbb"),array("x"=>"xxx","y"=>"yyy"));
$array2= array(array("c"=>"ccc"),array("z"=>"zzz"));
$i=0;
$j=0;
foreach($array2 as $single){
    foreach($array1 as $multiple){
        if($i == $j){
            $keys = array_keys($single);

            print $key = $keys[0];
            $multiple = array_push_assoc($multiple,$key,$single[$key]);
            $array1[$j] = $multiple;
        }
        ++$j;
    }
    ++$i;
}

var_dump($array1);

function array_push_assoc($array, $key, $value){
$array[$key] = $value;
return $array;
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM