$string = "1,2,3"
$ids = explode(',', $string);
var_dump($ids);
Run Code Online (Sandbox Code Playgroud)
回报
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
}
Run Code Online (Sandbox Code Playgroud)
我需要的值是int类型而不是string类型.有没有比使用foreach循环数组并将每个字符串转换为int更好的方法?
我想将一个字符串(例如1,2,3,4,5,6)转换为php中的整数数组?我找到的函数只能访问字符串的第一个字符,例如1.如何将整个字符串转换为数组?
function read_id_txt()
{
$handle_file = fopen("temporalfile.txt", 'r');
$i=0;
while ($array_var[$i] = fgets($handle_file, 4096)) {
echo "<br>";
print_r($array_var[i]);
$i++;
}
fclose($handle_file);
$temp=explode(" ", $array_var[0]);
return $temp;
}
Run Code Online (Sandbox Code Playgroud)