Another technique not yet mentioned on this page is to encode the array as a json string with the JSON_NUMERIC_CHECK
flag so that numeric values are appropriately cast as integers or floats. Then decode the generated string back to an array.
This approach does not require the developer to know in advance the key/index of the value nor require a conditional check on each value to see if the value is numeric. In simple terms, it is highly portable as a general-use strategy.
This technique conveniently accommodates arrays with more than one level.
Code: (Demo)
$array = [
"id",
"6086220176",
"6542925762",
"6623113406",
"6702782948",
['333', [['444']]]
];
var_export(
json_decode(
json_encode(
$array,
JSON_NUMERIC_CHECK
),
true
)
);
Output:
array (
0 => 'id',
1 => 6086220176,
2 => 6542925762,
3 => 6623113406,
4 => 6702782948,
5 =>
array (
0 => 333,
1 =>
array (
0 =>
array (
0 => 444,
),
),
),
)