0

我有 2 个数组。是否可以创建输出数组并将键/值从第一个添加到第二个?我的数组看起来像:

首先:

 Array
    (
        [0] => Array
            (
                [id] => 11
                [expire] => undefined
            )

        [1] => Array
            (
                [id] => 12
                [expire] => undefined
            )

        [2] => Array
            (
                [id] => 6
                [expire] => 8
            )

        [3] => Array
            (
                [id] => 10
                [expire] => 4
            )
    )

第二:

Array
    (
        [0] => Array
            (
                [id] => 6
                [realname] => to_es.gif
                [extension] => gif
                [filesize] => 57885
            )

        [1] => Array
            (
                [id] => 10
                [realname] => to_joomla_2_customer_view.gif
                [extension] => gif
                [filesize] => 77182
            )

        [2] => Array
            (
                [id] => 11
                [realname] => to_nl.gif
                [extension] => gif
                [filesize] => 10990
            )

        [3] => Array
            (
                [id] => 12
                [realname] => to_PL_1.gif
                [extension] => gif
                [filesize] => 52826
            )

    )

我如何获得输出数组:

 Array
        (
            [0] => Array
                (
                    [id] => 6
                    [realname] => to_es.gif
                    [extension] => gif
                    [filesize] => 57885
                    [expire] => 8
                )

            [1] => Array
                (
                    [id] => 10
                    [realname] => to_joomla_2_customer_view.gif
                    [extension] => gif
                    [filesize] => 77182
                    [expire] => 4
                )

            [2] => Array
                (
                    [id] => 11
                    [realname] => to_nl.gif
                    [extension] => gif
                    [filesize] => 10990
                    [expire] => undefined
                )

            [3] => Array
                (
                    [id] => 12
                    [realname] => to_PL_1.gif
                    [extension] => gif
                    [filesize] => 52826
                    [expire] => undefined
                )

        )
4

2 回答 2

1

我假设您的第一个数组$array1和第二个数组,因为$array2您可以在第一个数组中循环,然后在第二个比较中循环id,当找到时将两个数组混合在一个新数组中

$new_array = array();
for($i=0; $i<count($array1); $i++)
{
    foreach($array2 as $key => $data)
    {
        if($data['id'] == $array1[$i]['id'])
        {
            $new_array[] = array('id' => $array1[$i]['id'], 'realname' => $data['realname'], 'extension' => $data['extension'], 'filesize' => $data['filesize'], 'expire' => $array1[$i]['expire']);
        }
    }
}

var_dump($new_array);

这将输出

array(4) {
[0]=>
array(5) {
    ["id"]=>
    string(2) "11"
    ["realname"]=>
    string(9) "to_nl.gif"
    ["extension"]=>
    string(3) "gif"
    ["filesize"]=>
    string(5) "10990"
    ["expire"]=>
    string(9) "undefined"
}
  [1]=>
  array(5) {
    ["id"]=>
    string(2) "12"
    ["realname"]=>
    string(11) "to_PL_1.gif"
    ["extension"]=>
    string(3) "gif"
    ["filesize"]=>
    string(5) "52826"
    ["expire"]=>
    string(9) "undefined"
  }
  // and so on

实时工作代码

于 2013-05-15T12:16:04.863 回答
1

尝试这个:

function my_array_merge($first,$second) {
    $new = array();
    foreach ($first as $f_item) {
        foreach ($second as $i => $s_item) {
            if ($f_item['id']===$s_item['id']) {
                $new[] = $f_item + $s_item;
                unset($second[$i]);
                break;
            }
        }
    }
    usort($new, function($a,$b) {
        return $a['id'] - $b['id'];
    });
    return $new;
}

Ideone 示例:http: //ideone.com/TQIIkH

于 2013-05-15T12:23:35.427 回答