0

I have several arrays from $ _REQUEST. For example: $ _REQUEST ['name'] and $ _REQUEST ['email']. That is, inside these arrays there is also an array. It turns out an array in an array.
Assigned them value

$name = $_REQUEST['name'];
$email = $_REQUEST['email'];

now I need to remove the first key with the value. For this I used array_shift

array_shift($name);
array_shift($email);

so i got rid of the first value. But, besides the name and email, there are others. It would not be desirable for everyone to write array_shift. How can I apply to all with one function?
thanx

UPD
For example $_REQUEST array:

Array (
  [name] => Array (
    [0] => 
    [1] => myName
  )
  [email] => Array (
    [0] => 
    [1] => myEmail
  )
  [other] => Array (
    [0] => 
    [1] => otherDatas
  )
)

I must get rid of these empty elements

3
  • 1
    Provide array structure and expected output for clarity. Commented Nov 16, 2018 at 9:15
  • Its better to have a example array for us to play :) Commented Nov 16, 2018 at 9:17
  • Do you want to shift all arrays in $_REQUEST? Then you could just loop over them: foreach($_REQUEST as $r) array_shift($r); Or do you have a list/array of all variables that should be processed? Commented Nov 16, 2018 at 9:20

3 Answers 3

1

If I understand correctly, you have multiple arrays in your request and you want to do an array_shift on all of them?

You could loop through your $_REQUEST and apply that function to all the arrays. Maybe like this:

foreach ($_REQUEST as &$value) {
    if (is_array($value) && empty($value[0])) {
        array_shift($value);
    }
}

That will shift all arrays in your request and leave any other variables alone.

EDIT: Updated the example to only shift arrays where the first element is empty.

EDIT2: Added & to $value so that you can change the $_REQUEST variable directly.

Sign up to request clarification or add additional context in comments.

4 Comments

@Гани Шахмурат I've updated my example. Now it would only remove the first element for the arrays that have an empty first key.
@ГаниШахмурат this answer you should use
main.xfiddle.com/e6bb1d36/shift.php here's a sandbox with this code. something is not working.
That's because in that sandbox you print out the $_REQUEST again at the end. My code doesn't change the $_REQUEST variable. If you want to do that then you can use &$value. See my updated answer as well.
0

array_filter will clear empty values even if they are at any index.

$cleanArray = array();
foreach ($_REQUEST as $key => $value) {
    $cleanArray[$key] = array_filter($value);
}

Output

Array (
  [name] => Array (
    [1] => myName
  )
  [email] => Array (
    [1] => myEmail
  )
  [other] => Array (
    [1] => otherDatas
  )
)

Please note, it will also clear values like null, (blank) and false. Example Reference

Comments

0

What you might do is create an array with the keys that you want from $_REQUEST and use array_intersect_key to get your subset.

Then use array_map to check if the value is an array and return that value using array_filter to remove all values that you consider empty:

$keys = [
    "name",
    "email"
];

$result = array_map(function ($x) {
    if (is_array($x)) {
        return array_filter($x, function($y){
            return null !== $y && "" !== trim($y);
        });
    }
    return $x;
}, array_intersect_key($_REQUEST, array_flip($keys)));

print_r($result);

Demo

Comments

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.