2015-04-07 28 views
0

我有一個PHP類,如下(代碼的一部分):維護元素在PHP陣列和更新的PHP

class myclass{ 
    private static $arrX = array(); 

    private function is_val_exists($needle, $haystack) { 
     if(in_array($needle, $haystack)) { 
      return true; 
     } 
     foreach($haystack as $element) { 
      if(is_array($element) && $this->is_val_exists($needle, $element)) 
       return true; 
     } 
     return false; 
    } 

    //the $anInput is a string e.g. Michael,18 

    public function doProcess($anInput){ 

     $det = explode(",", $anInput); 

     if($this->is_val_exists($det[0], $this->returnProcess())){ 
      //update age of Michael 
     } 
     else{ 
      array_push(self::$arrX, array(
       'name' => $det[0], 
       'age' => $det[1] 
      )); 
     } 

    } 

    public function returnProcess(){ 
     return self::$arrX; 
    } 
} 

調用代碼index.php

$msg = 'Michael,18'; 
myclass::getHandle()->doProcess($msg); 

在我的網頁上說: index.php,它一遍又一遍地調用函數doProcess()。當函數被調用時,字符串被傳遞並存儲在一個數組中。在下一次通話中,如果讓我們說相同的名字再次通過,我想更新他的年齡。我的問題是我不知道如何檢查數組$arrX是否包含名稱。從我自己的發現來看,當代碼被調用時,數組似乎被重新啓動(回到零元素)。我的代碼永遠不會更新,並始終轉到array_push部分。希望有人能對此提出一些想法。謝謝。

+1

檢查的關鍵是在陣列中,檢查出ar​​ray_key_exists功能 - http://php.net/manual/en/function.array-key-exists.php :) – flauntster

+0

你可以分享'$ arrX'的一些數據樣本! –

+0

向我們顯示調用代碼 – niyou

回答

0

正如我在評論中所說,它看起來像你想保持請求之間的狀態。您不能使用純PHP來做到這一點,您應該使用外部存儲解決方案。如果可用,請嘗試Redis,它具有您所需要的且使用起來非常簡單。或者,如果您熟悉SQL,則可以使用MySQL。

在附註中,您應該詳細瞭解PHP數組的工作原理。 ,你可以只使用self::$arrX[] = ...

  • 取而代之的是

    1. 相反array_push的,你也可以使用一個關聯數組,例如self::$arrX[$det[0]] = $det[1];,這將使查找更容易(array_key_exists等)
  • +0

    '請求之間保持狀態'是關鍵。如果PHP不可行,那麼我會使用MySQL。我可以用SESSION來維護狀態/值,但我不想這樣做,因爲這可能會給內存帶來負擔。 – UserProg

    +0

    @UserProg你可以使用'$ _SESSION'是的,但正如名字所說,它是每個會話的存儲空間,也就是說,每個用戶都會有單獨的會話(和數據)。如果這是你所需要的,而不是一個全球性的國家,那就去做吧。 –

    +0

    明白了,但'$ _SESSION'可能不是我最好的選擇,因爲數據越來越大,我不希望它殺死內存。 MySQL是我的最佳選擇。感謝您的解釋:-) – UserProg

    1

    有一個)在您doProcess()功能的其他條件缺失,應改爲:

    else{ 
         array_push(self::$arrX, array(
          'name' => $det[0], 
          'age' => $det[1] 
         )); // <-- there was the missing) 
        } 
    

    這是基於你的代碼完整的運行方案:

    <?php 
    class myclass{ 
        private static $arrX = array(); 
    
        private function is_val_exists($needle, $haystack) { 
         if(in_array($needle, $haystack)) { 
          return true; 
         } 
         foreach($haystack as $element) { 
          if(is_array($element) && $this->is_val_exists($needle, $element)) 
           return true; 
         } 
         return false; 
        } 
    
        //the $anInput is a string e.g. Michael,18 
    
        public function doProcess($anInput){ 
    
         $det = explode(",", $anInput); 
    
         if($this->is_val_exists($det[0], $this->returnProcess())){ 
          //update age of Michael 
          for ($i=0; $i<count(self::$arrX); $i++) { 
           if (is_array(self::$arrX[$i]) && self::$arrX[$i]['name'] == $det[0]) { 
           self::$arrX[$i]['age'] = $det[1]; 
           break; 
           } 
          } 
         } else{ 
          array_push(self::$arrX, array(
           'name' => $det[0], 
           'age' => $det[1] 
          )); 
         } 
    
        } 
    
        public function returnProcess(){ 
         return self::$arrX; 
        } 
    } 
    
    $mc = new myclass(); 
    $mc->doProcess('Michael,18'); 
    $mc->doProcess('John,23'); 
    $mc->doProcess('Michael,19'); 
    $mc->doProcess('John,25'); 
    print_r($mc->returnProcess()); 
    
    ?> 
    

    你可以測試它在這裏:PHP Runnable

    +0

    感謝您的收穫,但問題仍然存在。 – UserProg

    +0

    @UserProg現在基於您的代碼添加了運行示例 –

    +0

    頁面不存在於php runnable – UserProg

    0

    你可以嘗試更新is_val_exists如下:

    private function is_val_exists($needle, $haystack) { 
    foreach($haystack as $element) { 
        if ($element['name'] == $needle) { 
        return true; 
    } 
    return false; 
    } 
    
    +0

    謝謝。該數組實際上是一個多維數組,所以這對我不起作用。 – UserProg

    +0

    它的二維數組rt? like array(0 => array('name'=>'saaaa','age'=> 25)); –

    +0

    我的錯誤,是的,它是二維數組。但是我想維護請求之間的數組的值。就像'SESSION'一樣,當它被反覆調用並被銷燬之前,該值不會被改變。欣賞你的答案:-) – UserProg