December 24, 201213 yr Hello I need to do the following and i need some help : these are the variable i have : $Variable1 = 09 $Variable2 = 06 $Variable3 = 12 $Variable4 = 16 I need to know how to print the content of these variables in an Incremental way like this : 06 09 12 16 Thank you in advance
December 24, 201213 yr How about echo $Variable1 . '<br />' . $Variable2 . '<br />' . $Variable3 . '<br />' . $Variable4 . '<br />' ;
December 25, 201213 yr Author How about echo $Variable1 . '<br />' . $Variable2 . '<br />' . $Variable3 . '<br />' . $Variable4 . '<br />' ; The result to that would be : 09 06 12 16 and i want : 06 09 12 16 in brief i want PHP to check and classify the variables from the minimum to to the maximum number that way it will echo the smallest number first, then the second, then the third and so on ... and i don't want to do that manually
December 26, 201213 yr you can try something like this <?php $array = array(); // you can change the numbers for variables i.e. //$array[] = $Variable1; $array[] = 9; $array[] = 6; $array[] = 12; $array[] = 16; array_multisort($array); echo $array[0].'<br />'; echo $array[1].'<br />'; echo $array[2].'<br />'; echo $array[3].'<br />'; ?> Edited December 26, 201213 yr by D4Y0
January 3, 201313 yr Both are the methode to print this one is loop or second is through arry. Would that be 'arry from Sham69's great hit?
January 3, 201313 yr Here you go: $vars = array("09","06","12","16"); sort($vars); foreach ($vars as $value) { echo $value."<br />"; } You can always convert them back to integer if you need them as integer. I put them as string to maintain the leading zero in the first 2 array elements. Hope this helps. Regards, L2c. Edited January 3, 201313 yr by Love2c0de
January 4, 201313 yr If you have integers instead of strings here is my advice: $Variables = array(9,6,12,26,112); sort($Variables); foreach ( $Variables as $v ){ echo str_pad($v, 2, 0, STR_PAD_LEFT)."<br />"; } The str_pad() function fills the zeros for you where you need, and if you want you can increase the number of zeros by increasing the second parameter of the function. (http://php.net/manual/en/function.str-pad.php)
January 4, 201313 yr Thanks for that. Always wondered how to work with integers but to keep the leading zeros. Great post. Kind regards, L2c.
Create an account or sign in to comment