如何更新数组的值?

我有这个变量$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);

任何想法都将是可观的。


有只小跳蛙
浏览 274回答 3
3回答

临摹微笑

该功能只有几个小改动。主要问题是您只是在本地副本中更改变量的值,您需要更改要通过引用传递的变量数组以允许它更新原始数据。&在参数前面添加$variables应该这样做。function updateTheValue(&$variables,$variable) {此外,当您设置值时,您将元素设置为仅包含值部分的新数组,而不是仅更新值,因此将该行更改为...$variables[trim($expbyequal[0])]["value"]= trim($expbyequal[1]);

LEATH

您的函数返回$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);使用此功能,您将更改主键中存在的键的数据值。您不需要使用$testing变量,因为引用会&改变您的主数组。

MYYA

希望这可以解决您对问题的回答:)$variable = "own=1,contract_start=1";function updateTheValue($variables,$variable) {&nbsp; &nbsp; // Split variable looking for into name and value&nbsp; &nbsp; $vars = explode(",", $variable);&nbsp; &nbsp; $match = false;&nbsp; &nbsp; foreach ($vars as $var){&nbsp; &nbsp; &nbsp; &nbsp; $expbyequal = explode("=", $var);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If this variable is set&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (array_key_exists($expbyequal[0],$variables)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Compare value with stored value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( $variables[trim($expbyequal[0])]['value'] == trim($expbyequal[1])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $match = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $variables[trim($expbyequal[0])]['value'] = trim($expbyequal[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $match = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $variables;}$variables = updateTheValue($variables,$variable);echo "<pre>";print_r($variables);
打开App,查看更多内容
随时随地看视频慕课网APP