如何更新数组的值?

How to update a value an array?

我有这个变量 $variable = "own=1,contract_start=1";,我想找到这个变量的数组。 数组名称是 $variables 这是我的数组:

Array
(
[own] => Array
    (
        [type] => bool
        [value] => 0
    )

[contr_name] => Array
    (
        [type] => bool
        [value] => 0
    )

[all_votes] => Array
    (
        [type] => int
        [value] => 0
    )

[contract_start] => Array
    (
        [type] => bool
        [value] => 0
    )

[contract_end] => Array
    (
        [type] => bool
        [value] => 0
    )

[T] => Array
    (
        [type] => clock
        [value] => 
    )

[a] => Array
    (
        [type] => int
        [value] => 1
    )

[candi_ID] => Array
    (
        [type] => int
        [value] => 1
    )

[voter_ID] => Array
    (
        [type] => int
        [value] => 1
    )

[] => Array
    (
        [type] => 
        [value] => 
    )

 )

如果该值不等于数组的现有值,那么我想用变量值更新该值。 这是我的代码:

$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 ( isset($variables [trim($expbyequal[0])]) )  {
            // 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;
}
$testing = updateTheValue($variables,$variable);

任何想法都将不胜感激。

仅对函数进行了一些小改动。

主要问题是您只是更改本地副本中的变量值,您需要更改要通过引用传递的变量数组以允许它更新原始数据。在参数 $variables 前添加 & 应该可以做到这一点。

function updateTheValue(&$variables,$variable) {

此外,当您设置值时,您不是仅仅更新值,而是将元素设置为仅包含值部分的新数组,因此将该行更改为...

$variables[trim($expbyequal[0])]["value"]= trim($expbyequal[1]);

您的函数 returns $match,可能是 truefalse,它不允许您看到您的更改。

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);

使用此功能,您将更改主键中存在的键的数据值。您不需要使用 $testing 变量,因为引用 & 会改变您的主数组。

Demo

希望这能解决您对问题的回答:)

$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);

你的问题很少。

首先 - 您需要通过引用传递 $variables,这样当您在函数内部更新它时,它会在全局范围内发生变化。

其次 - 当 "own=1,contract_start=1" 中的最后一个参数匹配时,您的 $match 显然只会是 true。我相信这不是你所期望的。不出所料,您在这里需要 "at least one parameter match" 或 "all parameter match" 逻辑。我在示例中使用了第二个。

<?php

$array = [
    "own" => [
        "type"  => "bool",
        "value" => 0
    ],

    "contr_name" => [
        "type"  => "bool",
        "value" => 0
    ],

    "all_votes" => [
        "type"  => "int",
        "value" => 0
    ],

    "contract_start" => [
        "type"  => "bool",
        "value" => 0
    ],

    "contract_end" => [
        "type"  => "bool",
        "value" => 0
    ],

    "T" => [
        "type"  => "clock",
        "value" => ""
    ],

    "a" => [
        "type"  => "int",
        "value" => 1
    ],

    "candi_ID" => [
        "type"  => "int",
        "value" => 1
    ],

    "voter_ID" => [
        "type"  => "int",
        "value" => 1
    ]
];

/** updateTheValue will return true if every parameter in $variable is
 *   exist in $variables and its values are the same.
 *   &$variables parameters send $variables "by reference"
 */
function updateTheValue(&$variables, $variable)
{
    $everyValueMatch = true;
    $variable = str_replace(",", "&", $variable);
    parse_str($variable, $parameters);

    foreach ($parameters as $parameterName => $parameterValue) {
        if (!isset($variables[$parameterName])) {
            $everyValueMatch = false;
            continue;
        }

        if ($variables[$parameterName]['value'] == $parameterValue) {
            continue;
        }

        $everyValueMatch = false;
        $variables[$parameterName]['value'] = $parameterValue;
    }
    return $everyValueMatch;
}

/** START */

$variable = "own=1,contract_start=1";

updateTheValue($array,$variable);


此外,将 "own=1,contract_start=1" 字符串转换为 url 就像字符串 "own=1&contract_start=1",然后使用 parse_str() 函数将其解析为数组更容易。