【问题标题】:Function $_POST return value from array parameter函数 $_POST 从数组参数返回值
【发布时间】:2017-02-19 19:56:47
【问题描述】:

我从方法 post 得到了这个响应

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['active'] 并从另一个数组索引中获取值

  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;
}

如果参数键是一个数组,则从这个键数组中获取值

例如 $_POST['a']['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);
}

【问题讨论】:

  • 只有在 param 和 $_POST 是字符串时才有效

标签: php arrays post


【解决方案1】:

我认为 post() 内的 line1 正在搞砸事情。根据$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;
}

!empty 条件检查 $result 中的 NOT 错误值。 查看所有错误值@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);
}

【讨论】:

  • 感谢您的回复,但如果我使用三维数组不起作用例如: request::post (数组("a","b","c");
  • @Offboard 我刚刚在我的服务器上测试了我的更新函数,它可以在多维数组的任何深度上工作。
  • 感谢我测试了多维数组及其工作,我改进了代码并达到了我的期望。
猜你喜欢
  • 2016-01-25
  • 1970-01-01
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
  • 2021-10-02
  • 2011-02-28
  • 2021-12-15
  • 2012-08-17
相关资源
最近更新 更多