我需要将数组分成两个数组.
一个数组将包含所有正值,其他所有负值和零将被视为正值.
示例数组:
$ts = array(7,-10,13,8,4,-7.2,-12,-3.7,3.5,-9.6,6.5,-1.7,-6.2,7);
不使用任何数组函数..
很简单通过数组循环并检查数字是否小于0,如果是,请将其推入负数组,否则将其推入正数组.
<?PHP $ts=array(7,7); $pos_arr=array(); $neg_arr=array(); foreach($ts as $val) { ($val<0) ? $neg_arr[]=$val : $pos_arr[]=$val; } print_r($pos_arr); print_r($neg_arr);
输出:
Array ( [0] => 7 [1] => 13 [2] => 8 [3] => 4 [4] => 3.5 [5] => 6.5 [6] => 7 ) Array ( [0] => -10 [1] => -7.2 [2] => -12 [3] => -3.7 [4] => -9.6 [5] => -1.7 [6] => -6.2 )