Get value of specific $_POST array

前端 未结 2 1119
栀梦
栀梦 2021-01-29 09:47
name=\"qty\"



foreach ($_POST as $items => $value)
{
  // check qty >1???
echo $key, \' => \', $value, \'
\'; }
2条回答
  •  滥情空心
    2021-01-29 10:40

    Why don't you add a check in your loop and then get a filtered output like this:

    if your POST request is not a multidimensional array then use this:

    $output = array_filter($_POST, function ($value, $key) {
            // check your keys start with 'qty' and its value is greater than one
            return strpos($key, 'qty') === 0 && $value > 1; 
        }, ARRAY_FILTER_USE_BOTH);
    
    // display final output
    print_r($output);
    

    If your POST request is a multidimensional array then use this:

    $output = array(); // declare a blank array to store filtered output
    
    foreach($_POST as $row)
        {
        if (is_array($row))
            {
    
            // this code is because qty is dynamic and we need to check it for all.
    
            foreach($row as $key => $value)
                {
                if ("qty" == substr($key, 0, 3) && $value > 0)
                    {
                    $output[] = $row;
                    }
                }
            }
        }
    
    // display final array.
    
    print_r($output);
    

    Hope that will help!

提交回复
热议问题