1

As shown in String to array of Integers php, there is a solution to convert ALL array elements to integer. But what if my array looks like this:

$v=array(0.00, "0.00", "test", 50);

I need a converted array where all numeric elements (even if quoted, like "0.00") are converted to integer, but strings (like "test") must remain strings...

Community
  • 1
  • 1
Dong3000
  • 566
  • 2
  • 7
  • 24

1 Answers1

1

Try with array_map. It will convert all the elements(except the strings you mentioned) to integer.

function convert_data($data) {
    if (is_numeric($data)) {
        $data = (int) $data;
    }
    return $data;
}
$ints[] = array_map('convert_data', $v);
var_dump($ints);
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87