1

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 Commented Mar 12, 2017 at 3:58

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank for your reply, but if I use tridimensional array not works ex: <input name="test['a']['b']['c']"> request::post(array("a","b","c");
@Offboard I have just tested my updated function on my server and it will work on any depth of a multidimensional array.
thanks I tested with multidimensional array and its works, I improve the code and met my expectations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.