Trim doesnt work in beforeFilter

Hello
Need help with this:

if ($this->request->is([‘get’]) && is_array($this->request->query)) {

        array_walk_recursive(($this->request->query, "trim");

        $this->request->query = $this->requestDataFilter($this->request->query, "GET");

    }

    if ($this->request->is(['patch', 'post', 'put']) && is_array($this->request->data)) {

        array_walk_recursive($this->request->data, "trim");

        $this->request->data = $this->requestDataFilter($this->request->data, "POST");

    }

I want to trim $this->request-query or data in beforeFilter but trim doesn’t work.

a few questions:

  1. is the typo in the first line actually in your code? ...usrive(($thi... should be ...ursive($thi...
  2. what does the callback trim look like?
  3. is the callback actually available in this scope? I guess it would have to be a function available in the global scope to be called in this way?

@dreamingmind, I’m assuming this is the standard PHP function trim that they’re using.

if so that won’t work because it doesn’t accept the arguments that the walk recursive callable will receive; item and key

from https://www.php.net/manual/en/function.array-walk-recursive.php#refsect1-function.array-walk-recursive-examples

$sweet = array('a' => 'apple', 'b' => 'banana');
$fruits = array('sweet' => $sweet, 'sour' => 'lemon');

function test_print($item, $key)
{
    echo "$key holds $item\n";
}

array_walk_recursive($fruits, 'test_print');

You’ll need to pass a different callable crafted to accept the two arguments array_walk_recursive() will pass to it:

array_walk_recursive($query, function ($value, $key) {
   return trim($value);
});

Comments in the manual for array_walk_recursive indicate that the callback should take a reference and change the value, not return anything.

True!

array_walk_recursive($query, function (&$value, $key) {
    $value = trim($value);
});

I also neglected making the first arg of the callback a reference to get the values back out.

thx for help.
array_walk_recursive($query, function (&$value, $key) {
$value = trim($value);
});
Work fine.