Search code examples
phpcallbackcallablearray-map

How to convert an array of number base 10 to base 16 use array_map and intval?


I try to convert an array of number to base 16 use intval and I want learn how to pass second argument to interval, in default intval use base 10.

array_map('intval', [1, 2, "12", "15" , "99"]);

Before I search in google and read php.net document but can't solve this problem.


Solution

  • You can define your own anonymous function:

    array_map(function ($value) { return intval($value, 16); }, [1, 2, "12", "15", "99"]);
    

    Demo: https://3v4l.org/PoFZN