我想合并二维数组

时间:2017-05-25 11:52:07

标签: php arrays array-merge

让我来描述一下这个场景 我有两个数组,第二列为0 另一个数组,第二列有一些值,

第一个数组

阵列

[0] => Array
    (
        [time_frame] => 2017-05-19
        [email_data] => 0
    )

[1] => Array
    (
        [time_frame] => 2017-05-20
        [email_data] => 0
    )

[2] => Array
    (
        [time_frame] => 2017-05-21
        [email_data] => 0
    )

[3] => Array
    (
        [time_frame] => 2017-05-22
        [email_data] => 0
    )

[4] => Array
    (
        [time_frame] => 2017-05-23
        [email_data] => 0
    )

[5] => Array
    (
        [time_frame] => 2017-05-24
        [email_data] => 0
    )

[6] => Array
    (
        [time_frame] => 2017-05-25
        [email_data] => 0
    )

第二个数组

阵列

[0] => Array
    (
        [time_frame] => 2017-05-20
        [email_data] => 1
    )

[1] => Array
    (
        [time_frame] => 2017-05-21
        [email_data] => 2
    )

[2] => Array
    (
        [time_frame] => 2017-05-24
        [email_data] => 8
    )

现在我想要实现的是以下数据:

[
{
    "time_frame": "2017-05-19",
    "email_data": "0"
},
{
    "time_frame": "2017-05-20",
    "email_data": "1"
},
{
    "time_frame": "2017-05-21",
    "email_data": "2"
},
{
    "time_frame": "2017-05-22",
    "email_data": "0"
},
{
    "time_frame": "2017-05-23",
    "email_data": "0"
},
{
    "time_frame": "2017-05-24",
    "email_data": "8"
},
{
    "time_frame": "2017-05-25",
    "email_data": "0"
}

提前致谢。

2 个答案:

答案 0 :(得分:1)

查看这段代码......

$data1 = array(
        array('time_frame'=>'2017-05-19', 'email_data'=>0),
        array('time_frame'=>'2017-05-20', 'email_data'=>0),
        array('time_frame'=>'2017-05-21', 'email_data'=>0),
        array('time_frame'=>'2017-05-22', 'email_data'=>0),
        array('time_frame'=>'2017-05-23', 'email_data'=>0),
        array('time_frame'=>'2017-05-24', 'email_data'=>0),
        array('time_frame'=>'2017-05-25', 'email_data'=>0)
    );


$data2 = array(
        array('time_frame'=>'2017-05-20', 'email_data'=>1),
        array('time_frame'=>'2017-05-21', 'email_data'=>2),
        array('time_frame'=>'2017-05-24', 'email_data'=>8)
    );


foreach ($data2 as $key2 => $value2)
{
    foreach ($data1 as $key1 => $value1)
    {
        if($value2['time_frame'] == $value1['time_frame']) {
            $data1[$key1] = $value2;
        }
    }
}

echo '<pre>';
print_r($data1);
echo '</pre>';

答案 1 :(得分:0)

为什么你使用循环,php提供array_merge()函数使用它 例子就在这里..

第一个数组..

$first = array(
        0 => Array
            (
                "time_frame" => "2017-05-20",
                "email_data" => "1"
            ),

        1 => Array
            (
                "time_frame" => "2017-05-21",
                "email_data" => "2"
            ),

        2 => Array
            (
                "time_frame" => "2017-05-24",
                "email_data" => "8"
            )
);

第二阵列......

$second = array(
            0 => Array
                (
                    "time_frame" => "2017-05-19",
                    "email_data" => 0
                ),

            1 => Array
                (
                    "time_frame" => "2017-05-20",
                    "email_data" => 0
                ),

            2 => Array
                (
                    "time_frame" => "2017-05-21"
                    ,"email_data" => 0
                ),

            3 => Array
                (
                    "time_frame" => "2017-05-22"
                    ,"email_data" => 0
                ),

            4 => Array
                (
                    "time_frame" => "2017-05-23"
                    ,"email_data" => 0
                ),

            5 => Array
                (
                    "time_frame" => "2017-05-24"
                    ,"email_data" => 0
                ),

            6 => Array
                (
                    "time_frame" => "2017-05-25"
                    ,"email_data" => 0
                )
    );

现在你的解决方案就在这里..

$combined_array = array_merge($first,$second);

它会给你所需的结果。简单。

打印结果......

foreach($combined_array as $key){
        print_r($key);
        echo "<br> - - - - - - - - - - - - - - -- - - - - - - <br>";
    }