PHP加入2阵列 [英] PHP join 2 arrays

查看:98
本文介绍了PHP加入2阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个阵列

  $ ARRAY1 =阵列(
    [2013年5月1日'] =>'测试',
    ['2013年5月2日'] =>'测试',
    [2013年5月3日'] =>'工作',
    [2013年5月4日'] =>'未来的测试');$数组2 =阵列(
    ['2013年5月1日'] =>'1',
    [2013年5月2日'] =>'做',
    [2013年5月3日'] =>'code',
    [2013年5月5日'] =>'释放');

我想加入这些阵列,使输出

  $结果=阵列(
        [2013年5月1日'] =>'测试1',
        ['2013年5月2日'] =>'测试2',
        [2013年5月3日'] =>'工作code',
        [2013年5月4日'] =>未来的测试,
        [2013年5月5日'] =>'释放')

我试过$结果= $数组1 +数组2; array_merge(),array_combine()没有给出正确的结果。

您可以帮我请。


解决方案

 的foreach($数组2 AS $关键=> $值)
{
    $数组1 [$关键] =使用isset($数组1 [$关键])? $数组1 [$关键。 。 $价值:$价值;
}

I have 2 arrays

$array1 = array(
    ['2013-05-01']=>'test',
    ['2013-05-02']=>'testing',
    ['2013-05-03']=>'working',
    ['2013-05-04']=>'future test');

$array2 = array(
    ['2013-05-01']=>'1',
    ['2013-05-02']=>'done',
    ['2013-05-03']=>'code',
    ['2013-05-05']=>'release');

I want to join these array, so that output is

$result = array(
        ['2013-05-01']=>'test 1', 
        ['2013-05-02']=>'testing 2',
        ['2013-05-03']=>'working code',
        ['2013-05-04']=>'future test',
        ['2013-05-05']=>'release')

I tried $result = $array1 + array2; array_merge() , array_combine() none gave the correct result.

Can you help me please.

解决方案

foreach($array2 AS $key => $value)
{
    $array1[$key] = isset($array1[$key]) ? $array1[$key] . " " . $value : $value;
}

这篇关于PHP加入2阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆