1
votes

I had this response from method 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"
  }
}

I need first use the foreach to get the $_POST['active'] and get value from another arrays index

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

the real problem is my function

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

if param key is an array get value from this key array

for example $_POST['a']['b']['c']

[EDIT]

I improve the function, thanks @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);
}
1
just works if param and $_POST is a string - Bruno Ribeiro

1 Answers

1
votes

I think line1 inside post() is fouling things up. Depending on $key's type, you are declaring $result's value as a key or a value. But then seemingly treating both possibilities as a value in the next condition and returning it.

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

The !empty condition is checking for NOT falsey values in $result. See all falsey values @ empty() manual, including 0 which can be a totally legitimate index/key, and more.

Instead, I think you want to use:

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