2017-02-19 99 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