0

I am trying to trim all values of arrays, which are located in an array, but so far I am not able to use array_walk_recursive properly - the values aren't trimmed after the array_walk_recursive is run. (in this case $test is returning true) This is my code so far:

$array = [

    [
      'component' => 'string          ',
      'productID' => 1,
      'no_cas' => '85085-34-3',
      'no_einecs' => '285-364-0',
      'isComponent' => true

    ],
    [
      'component' => 'string2       ',
      'productID' => 2,
      'no_cas' => '92128-34-2',
      'no_einecs' => '295-728-0',
      'isComponent' => true
    ],

];

function trimAll($item, $key){
    return trim($item);
}

$test = array_walk_recursive($array, 'trimAll');

var_dump('<pre>', $test , '</pre>');die;
4
  • 7
    function trimAll(&$item) {$item = trim($item);} Commented Jan 27, 2018 at 19:27
  • This works :). Thanks a lot for the help :) Commented Jan 27, 2018 at 19:29
  • And array_walk_recursive returns bool. Print $array instead. Commented Jan 27, 2018 at 19:30
  • @NiettheDarkAbsol Post that as an answer so Sasha can accept and close out his question :) Commented Jan 27, 2018 at 19:32

1 Answer 1

1

I would use:

array_map(
    function(array $a) {
        return array_map('trim', $a);
    },
    $array
);
Sign up to request clarification or add additional context in comments.

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.