【问题标题】:How to update a value an array?如何更新数组的值?
【发布时间】:2019-12-06 08:47:53
【问题描述】:

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

任何想法都会被认可。

【问题讨论】:

  • 您可以使用 in_array 函数检查数组值是否存在
  • 更改返回 $match;返回 $variables;
  • 如果你打算在这方面做更多的工作,我建议你研究一个更多的OO概念。您可以为不同的数据类型定义类,然后这将使值的验证成为代码的核心部分(您目前不做任何事情)。您还可以在开发需求时添加更复杂的类型。

标签: php arrays string isset


【解决方案1】:

对函数只有几个小改动。

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

function updateTheValue(&$variables,$variable) {

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

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

【讨论】:

    【解决方案2】:

    您的函数返回$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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        你的问题很少。

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

        第二 - 你的 $match 显然只有在 "own=1,contract_start=1" 中的最后一个参数匹配时才会是 true。我相信这不是你所期望的。期望您在此处需要“至少一个参数匹配”或“所有参数匹配”逻辑。我在示例中使用了第二个。

        <?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" 字符串转换为类似字符串"own=1&amp;contract_start=1" 的url,然后使用parse_str() 函数将其解析为数组更容易。

        【讨论】:

          猜你喜欢
          • 2022-07-07
          • 2017-09-19
          • 2019-11-25
          • 2021-10-14
          • 2019-03-14
          • 2017-05-20
          • 2021-08-19
          • 2019-03-04
          • 2019-02-12
          相关资源
          最近更新 更多