有没有办法在PHP中对数组执行此操作?

时间:2017-05-19 09:33:16

标签: php associative-array

我有两个数组

$array1[0]=array("account"=>002,"prin"=>100,"intr"=>50,"date"=>"2017-05-13");
$array1[1]=array("account"=>002,"prin"=>100,"intr"=>50,"date"=>"2017-05-20");

$array2[0]=array("account"=>002,"others"=>30,"date"=>"2017-05-13");
$array2[0]=array("account"=>002,"others"=>20,"date"=>"2017-05-13");
$array2[1]=array("account"=>002,"others"=>30,"date"=>"2017-05-20");
$array2[2]=array("account"=>002,"others"=>20,"date"=>"2017-05-20");

我想将这两个数组合并为一个这样的

$array3[0]=array("account"=>002,"prin"=>100,"intr"=>50,"date"=>"2017-05-13","others"=>30);
$array3[1]=array("account"=>002,"prin"=>'',"intr"=>'',"date"=>"2017-05-13","others"=>20);
$array3[3]=array("account"=>002,"prin"=>100,"intr"=>50,"date"=>"2017-05-20","others"=>30);
$array3[4]=array("account"=>002,"prin"=>'',"intr"=>'',"date"=>"2017-05-20","others"=>20);

我正在尝试array_merge,但它没有给我我想要的结果。 任何投入都会有很大的帮助。谢谢

2 个答案:

答案 0 :(得分:0)

可能这可以帮到你..

foreach($array2 as $key=>$value){
 if(isset($array1[$key])){
  array_merge($array1[$key],$array2[$key]);
 }
 else{
  $tempArray = array("account"=>'',"prin"=>'',"intr"=>'',"date"=>'');
  array_merge($array1[$key],$tempArray);
  array_merge($array1[$key],$array2[$key]);
 }
}

print_r($array1);

答案 1 :(得分:0)

您可以使用2个循环和the array_merge() function

CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
DateTime dt = DateTime.ParseExact("2017-06-02T00:00:00+05:30", "yyyy-MM-ddTHH:mm:sszzz", culture);

或者,由于您的数组具有字符串键(而不是数字键),因此您可以替换:

foreach($array1 as $item1)
{
    foreach($array2 as $item2)
    {
        if($item1['account'] == $item2['account'] && $item1['date'] == $item2['date'])
        {
            $array3[] = array_merge($item1, $item2);
        }
    }
}

人:

$array3[] = array_merge($item1, $item2);

它也会奏效。

示例输入:

$array3[] = $item1 + $item2;

对应输出:

$array1[0] = array('account' => '000', 'date' => '2017-04-30', 'prin' => 100, 'intr' => 50);
$array1[1] = array('account' => '001', 'date' => '2017-05-01', 'prin' => 101, 'intr' => 51);
$array1[2] = array('account' => '002', 'date' => '2017-05-02', 'prin' => 102, 'intr' => 52);

$array2[0] = array('account' => '001', 'date' => '2017-05-01', 'others' => 31); // Will match $array1[1]
$array2[1] = array('account' => '001', 'date' => '2017-05-01', 'others' => 21); // Will match $array1[1]
$array2[2] = array('account' => '002', 'date' => '2017-05-02', 'others' => 32); // Will match $array1[2]
$array2[3] = array('account' => '002', 'date' => '2017-05-02', 'others' => 22); // Will match $array1[2]
$array2[4] = array('account' => '001', 'date' => '1999-12-31', 'others' => 11); // Won't match anything in $array1, because of the date
$array2[5] = array('account' => '999', 'date' => '2017-05-02', 'others' => 99); // Won't match anything in $array1, because of the account
$array2[6] = array('account' => '001', 'date' => '2017-05-02', 'others' => 1); // Won't match $array1[1], because of the date, nor $array1[2] because of the account

注意:我不知道您的数据来自哪里,但如果它来自数据库,您最好在SQL查询中使用$array3[0] = array('account' => '001', 'date' => '2017-05-01', 'prin' => 101, 'intr' => 51, 'others' => 31); $array3[1] = array('account' => '001', 'date' => '2017-05-01', 'prin' => 101, 'intr' => 51, 'others' => 21); $array3[2] = array('account' => '002', 'date' => '2017-05-02', 'prin' => 102, 'intr' => 52, 'others' => 32); $array3[3] = array('account' => '002', 'date' => '2017-05-02', 'prin' => 102, 'intr' => 52, 'others' => 22);