php - Extract negative and positive value from an array to two separate arrays, -


i need divide array 2 arrays.

one array contain positive values, other negative values , 0 considered positive value.

example array:

$ts = array(7,-10,13,8,4,-7.2,-12,-3.7,3.5,-9.6,6.5,-1.7,-6.2,7); 

without using array functions..

pretty straightforward. loop through array , check if number less 0, if , push in negative array else push in positive array.

<?php $ts=array(7,-10,13,8,4,-7.2,-12,-3.7,3.5,-9.6,6.5,-1.7,-6.2,7); $pos_arr=array(); $neg_arr=array(); foreach($ts $val) {     ($val<0) ?  $neg_arr[]=$val : $pos_arr[]=$val; } print_r($pos_arr); print_r($neg_arr); 

output :

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 ) 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -