3 回答

TA貢獻1982條經(jīng)驗 獲得超2個贊
該功能只有幾個小改動。
主要問題是您只是在本地副本中更改變量的值,您需要更改要通過引用傳遞的變量數(shù)組以允許它更新原始數(shù)據(jù)。&
在參數(shù)前面添加$variables
應該這樣做。
function updateTheValue(&$variables,$variable) {
此外,當您設置值時,您將元素設置為僅包含值部分的新數(shù)組,而不是僅更新值,因此將該行更改為...
$variables[trim($expbyequal[0])]["value"]= trim($expbyequal[1]);

TA貢獻1936條經(jīng)驗 獲得超7個贊
您的函數(shù)返回$match,可能是trueor false,它不允許您查看更改。
function updateTheValue(&$variables,$variable) {
// Split variable looking for into name and value
$vars = explode(",", $variable);
$match = false;
foreach ($vars as $var){
$expbyequal = explode("=", $var);
// If this key exists in main keys
if ( in_array(trim($expbyequal[0]), array_keys($variables)) ) {
// Compare value with stored value
if ( $variables [trim($expbyequal[0])]['value'] == trim($expbyequal[1]) ) {
$match = true;
}else{
$variables[trim($expbyequal[0])]["value"] = trim($expbyequal[1]);
$match = false;
}
}
}
return $match;
}
updateTheValue($variables,$variable);
print_r($variables);
使用此功能,您將更改主鍵中存在的鍵的數(shù)據(jù)值。您不需要使用$testing變量,因為引用會&改變您的主數(shù)組。

TA貢獻1868條經(jīng)驗 獲得超4個贊
希望這可以解決您對問題的回答:)
$variable = "own=1,contract_start=1";
function updateTheValue($variables,$variable) {
// Split variable looking for into name and value
$vars = explode(",", $variable);
$match = false;
foreach ($vars as $var){
$expbyequal = explode("=", $var);
// If this variable is set
if (array_key_exists($expbyequal[0],$variables)){
// Compare value with stored value
if ( $variables[trim($expbyequal[0])]['value'] == trim($expbyequal[1])) {
$match = true;
}else{
$variables[trim($expbyequal[0])]['value'] = trim($expbyequal[1]);
$match = false;
}
}
}
return $variables;
}
$variables = updateTheValue($variables,$variable);
echo "<pre>";
print_r($variables);
- 3 回答
- 0 關(guān)注
- 297 瀏覽
添加回答
舉報