从数据库中检索数据:
$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',
),
),
这些是要求:
我的职能:
$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'];
}
}
我需要你的建议来实现目标。谢谢:))
答案 0 :(得分:1)
您可以使用另一个stackoverflow问题中的UDF in_array_r(感谢jwueller),并按照以下方式执行:
included_applications