2017-02-19 106 views
1

我不得不從方法交此響應

array(7) { 
    ["enable"]=> 
    array(2) { 
    [0]=> 
    string(2) "on" 
    [1]=> 
    string(2) "on" 
    } 
    ["value"]=> 
    array(2) { 
    [0]=> 
    string(8) "R$ 10,00" 
    [1]=> 
    string(8) "R$ 10,00" 
    } 
    ["zip_code"]=> 
    array(2) { 
    [0]=> 
    string(9) "57200-970" 
    [1]=> 
    string(9) "57200-990" 
    } 
    ["address"]=> 
    array(2) { 
    [0]=> 
    string(28) "Avenida Floriano Peixoto" 
    [1]=> 
    string(33) "Povoado Tabuleiro dos Negros" 
    } 
    ["neighborhood"]=> 
    array(2) { 
    [0]=> 
    string(6) "Centro" 
    [1]=> 
    string(4) "Bairro Vermelho" 
    } 
    ["city"]=> 
    array(2) { 
    [0]=> 
    string(6) "Penedo" 
    [1]=> 
    string(6) "Penedo" 
    } 
    ["state"]=> 
    array(2) { 
    [0]=> 
    string(2) "AL" 
    [1]=> 
    string(2) "AL" 
    } 
} 

我需要首先使用foreach得到$ _POST [「活性」]和來自另一陣列索引獲得值

foreach (Request::post('enable') as $k => $v) { 
    print Request::post(array("zip_code", $k)) . "\n"; 
    // I hope same result of $_POST['zip_code'][$k] 
    } 

真正的問題是我的功能

public static function post($key, $clean = false) { 
    $result = is_array($key) ? array_search($key, $_POST) : $_POST[$key]; 
    if (!empty($result)) { 
     return ($clean) ? trim(strip_tags($result)) : $result; 
    } 
    return NULL; 
} 

如果param鍵是一個數組,從這個關鍵AR獲得價值射線

例如$ _POST [ '一'] [ 'B'] [ 'C']

[編輯]

予改善的功能,由於@mickmackusa

public static function post($key, $clean = false) { 
    $focus = $_POST; 
    if (is_array($key)) { 
     foreach ($key as $k) { 
      if (!isset($focus[$k])) { 
       return NULL; 
      } 
      $focus = $focus[$k]; 
      if (!is_array($focus)) { 
       $result = $focus; 
      } 
     } 
    } else { 
     $result = empty($focus[$key]) ? NULL : $focus[$key]; 
    } 
    return ($clean ? trim(strip_tags($result)) : $result); 
} 
+0

只是工作如果param和$ _POST是一個字符串 – Offboard

回答

1

我認爲line1在post()裏面弄髒了東西。根據$key的類型,您聲明$result的值爲鍵或值。但似乎將這兩種可能性視爲下一個條件的價值並將其返回。

public static function post($key, $clean=false) { 
    $result = is_array($key) ? array_search($key, $_POST) : $_POST[$key]; 
    // if $key is an array, then $result is the key of the found value within $POST or FALSE if not found 
    // if $key is not an array, then $result is the value of $_POST[$key] 
    if (!empty($result)) { 
     return ($clean) ? trim(strip_tags($result)) : $result; 
    } 
    return NULL; 
} 

!空條件檢查$ result中的NOT falsey值。 查看所有falsey值@empty() manual,包括0,它可以是完全合法的索引/鍵,等等。

相反,我認爲你要使用:

public static function post($key,$clean=false){ 
    $focus=$_POST; 
    foreach($key as $k){ 
     if(!isset($focus[$k])){ 
      return NULL; 
     }else{ 
      $focus=$focus[$k]; 
      if(!is_array($focus)){ 
       $result=$focus; 
      } 
     } 
    } 
    return ($clean?trim(strip_tags($result)):$result); 
} 
+0

謝謝您的回覆,但如果我用立體陣列不能工作前: <輸入名稱= 「test ['a'] ['b'] ['c']」> request :: post(array(「a」,「b」,「c」); – Offboard

+0

@Offboard我剛剛測試了我的更新函數在我的服務器上,它可以工作在任何深度的多維數組上。 – mickmackusa

+0

謝謝我測試了多維數組及其工作,我改進了代碼並滿足了我的期望。 – Offboard