【问题标题】:Is there a way I can do this to an array in PHP?有没有办法可以对 PHP 中的数组执行此操作?
【发布时间】:2017-05-19 09:33:16
【问题描述】:

我有两个数组

$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,但它没有给我想要的结果。 任何输入都会有很大帮助。谢谢

【问题讨论】:

  • 使用php的array_merge()函数
  • 你试过array_merge_recursive()php.net/manual/en/function.array-merge-recursive.php吗?
  • array2怎么会有两个0索引
  • @SaurabhParekh 他已经提到他遇到了 array_merge 的问题,那你为什么还要提到同样的问题?
  • 我不清楚您要合并哪些数组。你可以再详细一点吗?是基于“account”和“date”的值吗?

标签: php associative-array


【解决方案1】:

也许这对你有帮助..

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);

【讨论】:

  • 谢谢Zaheer,它给我的结果似乎和raw array_merge一样
【解决方案2】:

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

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

对应的输出:

$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);

注意:我不知道您的数据来自哪里,但如果它来自数据库,最好在 SQL 查询中使用 JOIN

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 2015-05-23
    相关资源
    最近更新 更多