我有2個陣列
我希望以$today為主,當 PH碼(例如 PH076262 )在 $yesterday 有看到時,$today 的 那筆資料變成
'0' => '1|PH076262|1'
'1' => '2|PH085771|0'
'2' => '3|PH039553|1'
'3' => '4|PH006735|1'
以此類推
$today = Array
(
'0' => '1|PH076262|0',
'1' => '2|PH085771|0',
'2' => '3|PH039553|0',
'3' => '4|PH006735|0',
'4' => '5|PH073642|0',
'5' => '6|PH047611|0',
'6' => '7|PH051917|0',
'7' => '8|PH050966|0',
'8' => '9|PH053062|0',
'9' => '10|PH073196|0',
'10' => '11|PH082607|0',
'11' => '12|PH083277|0',
'12' => '13|PH085436|0',
'13' => '14|PH085438|0',
'14' => '15|PH084794|0',
'15' => '16|PH071028|0',
'16' => '17|PH045859|0',
'17' => '18|PH063624|0'
);
$yesterday = Array
(
'0' => '1|PH045859|0',
'1' => '2|PH068211|0',
'2' => '3|PH084794|0',
'3' => '4|PH051917|0',
'4' => '5|PH085436|0',
'5' => '6|PH085569|0',
'6' => '7|PH063624|0',
'7' => '8|PH047611|0',
'8' => '9|PH039553|0',
'9' => '10|PH082607|0',
'10' => '11|PH046000|0',
'11' => '12|PH050966|0',
'12' => '13|PH006735|0',
'13' => '14|PH076262|0',
'14' => '15|PH085568|0',
'15' => '16|PH085438|0',
'16' => '17|PH073196|0',
'17' => '18|PH073642|0',
'18' => '19|PH053062|0'
);
請問怎麼做比較好呢?
謝謝
我也是先建一個參考表,再去表裡查。不過這樣寫,沒用到 Regular Expression 應該快上數十倍,而且 php v3 以上就能跑:
<pre class="c" name="code"><?php
$today = Array
(
'0' => '1|PH076262|0',
'1' => '2|PH085771|0',
'2' => '3|PH039553|0',
'3' => '4|PH006735|0',
'4' => '5|PH073642|0',
'5' => '6|PH047611|0',
'6' => '7|PH051917|0',
'7' => '8|PH050966|0',
'8' => '9|PH053062|0',
'9' => '10|PH073196|0',
'10' => '11|PH082607|0',
'11' => '12|PH083277|0',
'12' => '13|PH085436|0',
'13' => '14|PH085438|0',
'14' => '15|PH084794|0',
'15' => '16|PH071028|0',
'16' => '17|PH045859|0',
'17' => '18|PH063624|0'
);
$yesterday = Array
(
'0' => '1|PH045859|0',
'1' => '2|PH068211|0',
'2' => '3|PH084794|0',
'3' => '4|PH051917|0',
'4' => '5|PH085436|0',
'5' => '6|PH085569|0',
'6' => '7|PH063624|0',
'7' => '8|PH047611|0',
'8' => '9|PH039553|0',
'9' => '10|PH082607|0',
'10' => '11|PH046000|0',
'11' => '12|PH050966|0',
'12' => '13|PH006735|0',
'13' => '14|PH076262|0',
'14' => '15|PH085568|0',
'15' => '16|PH085438|0',
'16' => '17|PH073196|0',
'17' => '18|PH073642|0',
'18' => '19|PH053062|0'
);
foreach ($yesterday as $y)
{
list(,$i) = explode('|', $y);
$ya[$i] = true;
}
foreach ($today as $k => $v)
{
list($i1, $i2, $i3) = explode('|', $v);
if (isset($ya[$i2])) $today[$k] = $i1 . '|' . $i2 . '|1';
}
print_r($today);
?>
我沒想到比較快的方法
就只能把 $yesterday 的值先建個列表
然後一個個比對 $today 的值是否有在表中
以下程式需要 php 5.3 以上才能跑
<pre class="c" name="code">
<?php
$today = Array
(
'0' => '1|PH076262|0',
'1' => '2|PH085771|0',
'2' => '3|PH039553|0',
'3' => '4|PH006735|0',
'4' => '5|PH073642|0',
'5' => '6|PH047611|0',
'6' => '7|PH051917|0',
'7' => '8|PH050966|0',
'8' => '9|PH053062|0',
'9' => '10|PH073196|0',
'10' => '11|PH082607|0',
'11' => '12|PH083277|0',
'12' => '13|PH085436|0',
'13' => '14|PH085438|0',
'14' => '15|PH084794|0',
'15' => '16|PH071028|0',
'16' => '17|PH045859|0',
'17' => '18|PH063624|0'
);
$yesterday = Array
(
'0' => '1|PH045859|0',
'1' => '2|PH068211|0',
'2' => '3|PH084794|0',
'3' => '4|PH051917|0',
'4' => '5|PH085436|0',
'5' => '6|PH085569|0',
'6' => '7|PH063624|0',
'7' => '8|PH047611|0',
'8' => '9|PH039553|0',
'9' => '10|PH082607|0',
'10' => '11|PH046000|0',
'11' => '12|PH050966|0',
'12' => '13|PH006735|0',
'13' => '14|PH076262|0',
'14' => '15|PH085568|0',
'15' => '16|PH085438|0',
'16' => '17|PH073196|0',
'17' => '18|PH073642|0',
'18' => '19|PH053062|0'
);
function PH_merge($today, $yesterday) {
$yrec = array_map(
function($e){
return preg_match('/\|(.*)\|/', $e, $m) ? $m[1] : null;
},
$yesterday
);
return array_map(
function($e) use($yrec) {
return preg_match('/^.*\|(.*)\|/', $e, $m) ? (in_array($m[1], $yrec) ? "$m[0]1" : $e) : $e;
},
$today
);
}
$result = PH_merge($today, $yesterday);
print_r($result);
輸出:
<pre class="c" name="code">
Array
(
[0] => 1|PH076262|1
[1] => 2|PH085771|0
[2] => 3|PH039553|1
[3] => 4|PH006735|1
[4] => 5|PH073642|1
[5] => 6|PH047611|1
[6] => 7|PH051917|1
[7] => 8|PH050966|1
[8] => 9|PH053062|1
[9] => 10|PH073196|1
[10] => 11|PH082607|1
[11] => 12|PH083277|0
[12] => 13|PH085436|1
[13] => 14|PH085438|1
[14] => 15|PH084794|1
[15] => 16|PH071028|0
[16] => 17|PH045859|1
[17] => 18|PH063624|1
)