從$hasil
我的數組是這樣存儲陣列具有相同的值
Array ([24] => 0.29883576167978 [20] => 0.29883576167978 [20,24] => 0.17930145700787 [34] => 0.12390406380914 [26,34] => 0.099123251047315)
我嘗試這樣
$newArray = array();
$i = 0;
foreach ($hasil as $key => $value) {
$newArray[$i] = $key . '|' . $value;
$i++;
}
for ($i = 0; $i < sizeof($newArray); $i++) {
for ($j = 0; $j < sizeof($newArray) - 1; $j++) {
$n1 = explode("|", $newArray[$j]);
$n2 = explode("|", $newArray[$j+1]);
if (doubleval($n1[1]) < doubleval($n2[1])) {
$temp = $newArray[$j];
$newArray[$j] = $newArray[$j + 1];
$newArray[$j + 1] = $temp;
}
}
}
$idObjeks = array();
$i = 0;
foreach ($newArray as $key) {
$ex1 = explode("|", $key);
$ex2 = explode(",", $ex1[0]);
foreach ($ex2 as $ky) {
$idObjeks[$i] = $ky;
$i++;
}
}
$hasil = array();
foreach ($newArray as $key) {
$ex = explode("|", $key);
$hasil[$ex[0]] = $ex[1];
}
// I think wrong in this code ***
// I want save $hasil with the biggest or the biggest with same value in database
// when I print_r there are not the same value and the biggest value
$sameValue = array();
for ($i = 0; $i < sizeof($hasil) - 1; $i++) {
if (current($hasil) == next($hasil)) {
$sameValue[$i]['key'] = key($hasil);
$sameValue[$i]['value'] = current($hasil);
$sameValue[$i+1]['key'] = key($hasil);
$sameValue[$i+1]['value'] = next($hasil);
} else {
prev($hasil);
$sameValue[$i]['key'] = key($hasil);
$sameValue[$i]['value'] = current($hasil);
break;
}
}
,並存儲在數據庫中是這樣的。
$hasilInsert = array();
foreach ($sameValue as $key => $value) {
$hasilInsert = array(
'id_wi' => $wisatawan->id,
'hasil' => $value['key'],
'nilai' => $value['value']
);
$xy = HasilWisataTemp::insert($hasilInsert);
}
這段代碼有什麼問題?或者任何簡單的方法來在數據庫中存儲相同的值數組?因爲我想存儲數組只是相同的值。
感謝您的關注。
什麼是你的問題? – Chris
如何使數組保存相同的值? –
你可以展示你已經嘗試過什麼,失敗了嗎?你有什麼*好像*好 – Chris