1

retrieve data from database :

$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 to merge with:

$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 result:

$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',
    ),
  ),

my result so far:

$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',
    ),
  ),

these are the requirements:

  1. if record in new array does not has pid (price id) then it's considered as new data. (record with start : 2015-06-03 and end : 2015-06-03)
  2. if record in new array has pid and match with one of record from table, then new record's values will override the old one (record with pid : 231096235 and pid : 231096236)
  3. if record from database has pid and does not match with any pid from new array then it should be added to new array (it shouldb record with pid : 231096234)

my functions :

$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'];
    }
}

I need your advise to achieve the goal. thanks :)

Fatimah Wulandari
  • 307
  • 2
  • 5
  • 16

1 Answers1

1

You can use the UDF in_array_r from another stackoverflow question (thanks to jwueller) and do it like this:

$merge = $new;

foreach($db as $item) {
    if (!in_array_r($item["pid"], $new)) {
        $merge[] = $item;
    }
}

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}
Community
  • 1
  • 1
Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62