How can I merge PHP arrays?

前端 未结 10 1794
盖世英雄少女心
盖世英雄少女心 2020-12-20 12:43

I have two arrays of animals (for example).

$array = array(
    array(
        \'id\' => 1,
        \'name\' => \'Cat\',
    ),
    array(
        \'id         


        
相关标签:
10条回答
  • 2020-12-20 13:12

    First off, why don't you use the ID as the index (or key, in the mapping-style array that php arrays are imo)?

    $array = array(
        1 => array(
            'name' => 'Cat',
        ),
        2 => array(
            'name' => 'Mouse',
        )
    );
    

    after that you'll have to foreach through one array, performing array_merge on the items of the other:

    foreach($array2 as $key=>$value) {
      if(!is_array($array[$key])) $array[$key] = $value;
      else $array[$key] = array_merge($array[key], $value); 
    }
    

    Something like that at least. Perhaps there's a better solution?

    0 讨论(0)
  • 2020-12-20 13:19
    <?php
    $array1 = array("color" => "red", 2, 4);
    $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
    $result = array_merge($array1, $array2);
    print_r($result);
    ?>
    
    0 讨论(0)
  • 2020-12-20 13:26

    This does what Erik suggested (id no. as array key) and merges vlaues in $array2 to $results.

    $results = array();
    
    foreach($array as $subarray)
    {
        $results[$subarray['id']] = array('name' => $subarray['name']);
    }
    
    foreach($array2 as $subarray)
    {
        if(array_key_exists($subarray['id'], $results))
        {
            // Loop through $subarray would go here if you have extra 
            $results[$subarray['id']]['age'] = $subarray['age'];
        }
    }
    
    0 讨论(0)
  • 2020-12-20 13:27
    foreach ($array as $a)
        $new_array[$a['id']]['name'] = $a['name'];
    
    foreach ($array2 as $a)
        $new_array[$a['id']]['age'] = $a['age'];
    

    and this is result:

        [1] => Array
            (
                [name] => Cat
                [age] => 123
            )
    
        [2] => Array
            (
                [name] => Mouse
                [age] => 321
            )
    
    0 讨论(0)
  • 2020-12-20 13:28
    $new = array();
    foreach ($array as $arr) {
        $match = false;
        foreach ($array2 as $arr2) {
            if ($arr['id'] == $arr2['id']) {
               $match = true;
               $new[] = array_merge($arr, $arr2);
               break;
            }
        }
        if ( !$match ) $new[] = $arr;
    }
    
    0 讨论(0)
  • 2020-12-20 13:29

    @Andy

    I've already looked at that and didn't see how it can help merge multidimensional arrays. Maybe you could give an example.

    @kevin

    That is probably what I will need to do as I think the code below will be very slow. The actual code is a bit different because I'm using ADOdb (and ODBC for the other query) but I'll make it work and post my own answer.

    This works, however I think it will be very slow as it goes through the second loop every time:

    foreach($array as &$animal)
    {
        foreach($array2 as $animal2)
        {
            if($animal['id'] === $animal2['id'])
            {
                $animal = array_merge($animal, $animal2);
                break;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题