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...

1 Answer 1

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);
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.