如何使用多维数组在PHP中将值分别显示为字符串,整数和双精度?
这是我的代码:
<?php
$array = array(
array ("one"=>1,"two"=>"two","three"=>3.1,"four"=>4,"five"=>"five"),
array ("six"=>6.1,"seven"=>7,"eight"=>"eight","nine"=>9.1,"ten"=>10),
array ("eleven"=>"eleven","twelve"=>12.1,"thirteen"=>13,"fourteen"=>"fourteen","fifteen"=>15.1),
array ("sixteen"=>16,"seventeen"=>"seventeen","eighteen"=>18.1,"nineteen"=> 19,"twenty"=>"twenty"),
array ("twenty-one"=>21.1,"twenty-two"=>22,"twenty-three"=>"twenty-three","twenty-four"=>24.1,"twenty-five"=>25)
);
答案 0 :(得分:0)
使用两个foreach语句,一个获取每个数组,一个循环获取值并使用switch / case确定值,并根据它将此值添加到该类型的数组
$integer=array();
$double=array();
$string=array()
foreach ($rows as $row) {
foreach($row as $key=>$value){
switch (true){
case is_string($value):
// string
$string[]=$value;
break;
case is_int($value):
// int
$integer[]=$value;
break;
case is_float($value);
// double
$double[]=$value;
break;
default:
}
}
}
答案 1 :(得分:0)
$values=array('int'=>array(), 'double'=>array(), 'string'=>array());
foreach ($rows as $row) {
foreach($row as $key=>$value){
switch (true){
case is_string($value):
$values['string'][]=$value;
break;
case is_int($value):
$values['int'][]=$value;
break;
case is_float($value);
$values['double'][]=$value;
break;
default:
}
}
}