在PHP中,可以使用不同的方法来提取数组中的不同类型的数据。
$array = [1, "two", 3.14, true];
$integers = [];
$strings = [];
$floats = [];
$booleans = [];
foreach ($array as $value) {
if (is_int($value)) {
$integers[] = $value;
} elseif (is_string($value)) {
$strings[] = $value;
} elseif (is_float($value)) {
$floats[] = $value;
} elseif (is_bool($value)) {
$booleans[] = $value;
}
}
// 输出提取的数据
print_r($integers);
print_r($strings);
print_r($floats);
print_r($booleans);
$array = [1, "two", 3.14, true];
$integers = array_filter($array, function ($value) {
return is_int($value);
});
$strings = array_filter($array, function ($value) {
return is_string($value);
});
$floats = array_filter($array, function ($value) {
return is_float($value);
});
$booleans = array_filter($array, function ($value) {
return is_bool($value);
});
// 输出提取的数据
print_r($integers);
print_r($strings);
print_r($floats);
print_r($booleans);
以上两种方法都可以根据数组中的数据类型提取不同类型的数据。
上一篇:php怎么读取二维数组有几行
下一篇:php中string怎么转浮点数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站