How to trim white spaces of array values in php

后端 未结 12 1421
旧巷少年郎
旧巷少年郎 2020-11-28 19:30

I have an array as follows

$fruit = array(\'  apple \',\'banana   \', \' , \',     \'            cranberry \');

I want an array which conta

12条回答
  •  旧时难觅i
    2020-11-28 20:18

    array_map('trim', $data) would convert all subarrays into null. If it is needed to trim spaces only for strings and leave other types as it is, you can use:

    $data = array_map(
        function ($item) {
            return is_string($item) ? trim($item) : $item;
        },
        $data
    );
    

提交回复
热议问题