合并两个数组合并相同的数据添加新数据

时间:2015-06-11 03:24:42

标签: php arrays

从数据库中检索数据:

$db = array (
    0 => 
    array (
      'stay' => '10',
      'end' => '2015-12-29',
      'start' => '2015-12-20',
      'pid' => '231096236',
      'normal_price' => '385',
      'promo_price' => '385',
      'resell_price' => '385',
    ),
    1 => 
    array (
      'stay' => '7',
      'end' => '2016-01-05',
      'start' => '2015-12-30',
      'pid' => '231096235',
      'normal_price' => '385',
      'promo_price' => '385',
      'resell_price' => '385',
    ),
    2 => 
    array (
      'stay' => '3',
      'end' => '2053-01-01',
      'start' => '2016-01-06',
      'pid' => '231096234',
      'normal_price' => '385',
      'promo_price' => '385',
      'resell_price' => '385',
    ),
  ),

要合并的新数组:

$new = array (
    0 => 
    array (
      'stay' => '2',
      'end' => '2015-07-01',
      'start' => '2015-06-03',
      'normal_price' => '145',
      'promo_price' => '145',
      'resell_price' => '135',
    ),
    1 => 
    array (
      'stay' => '4',
      'end' => '2015-07-05',
      'start' => '2015-07-02',
      'pid' => '231096235',
      'normal_price' => '100',
      'promo_price' => '100',
      'resell_price' => '100',
    ),
    2 => 
    array (
      'stay' => '4',
      'end' => '2015-09-03',
      'start' => '2015-07-06',
      'pid' => '231096236',
      'normal_price' => '100',
      'promo_price' => '100',
      'resell_price' => '100',
    ),
  ),

预期结果:

$expected = array (
    0 => 
    array (
      'stay' => '2',
      'end' => '2015-07-01',
      'start' => '2015-06-03',
      'normal_price' => '145',
      'promo_price' => '145',
      'resell_price' => '135',
    ),
    1 => 
    array (
      'stay' => '4',
      'end' => '2015-07-05',
      'start' => '2015-07-02',
      'pid' => '231096235',
      'normal_price' => '100',
      'promo_price' => '100',
      'resell_price' => '100',
    ),
    2 => 
    array (
      'stay' => '4',
      'end' => '2015-09-03',
      'start' => '2015-07-06',
      'pid' => '231096236',
      'normal_price' => '100',
      'promo_price' => '100',
      'resell_price' => '100',
    ),
    3 => 
    array (
      'stay' => '3',
      'end' => '2053-01-01',
      'start' => '2016-01-06',
      'pid' => '231096234',
      'normal_price' => '385',
      'promo_price' => '385',
      'resell_price' => '385',
    ),
  ),

到目前为止的结果:

$merge = array (
    0 => 
    array (
      'stay' => '2',
      'end' => '2015-07-01',
      'start' => '2015-06-03',
      'normal_price' => '145',
      'promo_price' => '145',
      'resell_price' => '135',
    ),
    1 => 
    array (
      'stay' => '4',
      'end' => '2015-07-05',
      'start' => '2015-07-02',
      'pid' => '231096235',
      'normal_price' => '100',
      'promo_price' => '100',
      'resell_price' => '100',
    ),
    2 => 
    array (
      'stay' => '4',
      'end' => '2015-09-03',
      'start' => '2015-07-06',
      'pid' => '231096236',
      'normal_price' => '100',
      'promo_price' => '100',
      'resell_price' => '100',
    ),
  ),

这些是要求:

  1. 如果新数组中的记录没有pid(价格ID)那么它就是 被视为新数据。 (记录开始时间:2015-06-03并结束: 2015年6月3日)
  2. 如果新数组中的记录具有pid并且与表中的记录之一匹配,则新记录的值将覆盖旧记录(记录为pid:231096235和pid:231096236)
  3. 如果来自数据库的记录有pid并且与新数组中的任何pid不匹配,那么它应该被添加到新数组中(它应该用pid记录:231096234)
  4. 我的职能:

    $merge = array();
    foreach ($new as $ikey => $ival) {
        $ispid = (isset($ival['pid'])) ? (int) $ival['pid'] : 0;
        if ($ispid === 0) {
            $merge[] = $ival;
        }
        if ($ispid > 0) {
            $exist = false;
            foreach ($db as $rkey => $rval) {
                $rspid = (int) $rval['pid'];
                if ($ispid === $rspid) {
                    $spid_exist = true;
                    overridePriceValue($ival, $rval);
                    $merge[] = $rval;
                }
            }
    //        if ($exist === false) {
    //            echo "price id " . $ispid . " is not match with any special prices.";
    //        }
        }
    }
    
    function overridePriceValue($input_spc_price, &$spc_price) {
        if (isset($input_spc_price['stay'])) {
            $spc_price['stay'] = $input_spc_price['stay'];
        }
        if (isset($input_spc_price['end'])) {
            $spc_price['end'] = $input_spc_price['end'];
        }
        if (isset($input_spc_price['start'])) {
            $spc_price['start'] = $input_spc_price['start'];
        }
        if (isset($input_spc_price['normal_price'])) {
            $spc_price['normal_price'] = $input_spc_price['normal_price'];
        }
        if (isset($input_spc_price['promo_price'])) {
            $spc_price['promo_price'] = $input_spc_price['promo_price'];
        }
        if (isset($input_spc_price['resell_price'])) {
            $spc_price['resell_price'] = $input_spc_price['resell_price'];
        }
    }
    

    我需要你的建议来实现目标。谢谢:))

1 个答案:

答案 0 :(得分:1)

您可以使用另一个stackoverflow问题中的UDF in_array_r(感谢jwueller),并按照以下方式执行:

included_applications