2015-06-20 47 views
-1

我想編輯一個值,如$class->set("this.is.a.children.key", "and this is my value")我覺得編輯數組的值像set(「a.b.c.d.e」,「我的新值」),但不工作

我的功能:

public function set ($key, $value) { 
    if (array_key_exists($key, $this->config)) { 
     $this->config[$key] = $value; 
     return true; 
    } else { 
     if(strpos($key, ".")){ 
      $keys = explode(".", $key); 
      if (count($keys) > 0) { 
       if (array_key_exists($keys[0], $this->config)) { 
        function rv($source, $array_keys, $value){ 
         if (count($array_keys) == 1) { 
          $source[$array_keys[0]] = $value; 
         } else { 
          return rv(
           $source[$array_keys[0]], 
           array_slice($array_keys, 1), 
           $value 
          ); 
         } 
        } 
        $this->config = rv($this->config, $keys, $value); 
       } 
      } 
     } 
    } 
    return false; 
} 

我的數組:

$this->config = array(
    "a"=>array(
     "b"=>1, 
     "c"=>array(
      "d"=>array(
       "e"=>2, 
       "f"=>"x", 
       "g"=>null, 
      ) 
     ), 
     "h"=>null, 
     "i"=>102.2, 
     "j"=>array(
      3=>3 
     ) 
    ), 
    "k"=>":)" 
); 

我的一句話:

$this->set("k", "This is K"); 
$this->set("a.c.d.g", "This is a -> c -> d -> g"); 

回答

1

當您在rv方法中,你會修改$source陣列必須讓它成爲一個參考。出於同樣的原因,您還應該刪除$this->config = ...作業。

class Foo 
{ 
    public $config = array(
     "a" => array(
      "b" => 1, 
      "c" => array(
       "d" => array(
        "e" => 2, 
        "f" => "x", 
        "g" => null, 
       ) 
      ), 
      "h" => null, 
      "i" => 102.2, 
      "j" => array(
       3 => 3 
      ) 
     ), 
     "k" => ":)" 
    ); 

    public function set($key, $value) 
    { 
     if (array_key_exists($key, $this->config)) { 
      $this->config[$key] = $value; 
      return true; 
     } else { 
      if (strpos($key, ".")) { 
       $keys = explode(".", $key); 
       if (count($keys) > 0) { 
        if (array_key_exists($keys[0], $this->config)) { 
         // Added & to $source here 
         function rv(&$source, $array_keys, $value) 
         { 
          if (count($array_keys) == 1) { 
           $source[$array_keys[0]] = $value; 
          } else { 
           // No need for return statement 
           rv(
            $source[$array_keys[0]], 
            array_slice($array_keys, 1), 
            $value 
           ); 
          } 
         } 

         // No assignment 
         rv($this->config, $keys, $value); 
        } 
       } 
      } 
     } 
     return false; 
    } 

    public function test() 
    { 
     $this->set("k", "This is K"); 
     $this->set("a.c.d.g", "This is a -> c -> d -> g"); 
     print_r($this->config); 
    } 
} 

(new Foo())->test(); 

輸出:

Array 
(
    [a] => Array 
     (
      [b] => 1 
      [c] => Array 
       (
        [d] => Array 
         (
          [e] => 2 
          [f] => x 
          [g] => This is a -> c -> d -> g 
         ) 

       ) 

      [h] => 
      [i] => 102.2 
      [j] => Array 
       (
        [3] => 3 
       ) 

     ) 

    [k] => This is K 
) 
+0

這對我來說沒有工作......這無可代替陣列 –

+0

真奇怪。適用於PHP 5.5.20。嘗試添加'error_reporting(E_ALL | E_STRICT); ini_set('display_errors',1);'在文件頂部,如果你還沒有這樣做,看看你是否得到任何錯誤消息。 – mhall

相關問題