how to change type of value in an php array and sorting it..is it possible ?
Posted
by justjoe
on Stack Overflow
See other posts from Stack Overflow
or by justjoe
Published on 2010-03-27T09:45:10Z
Indexed on
2010/03/27
9:53 UTC
Read the original article
Hit count: 227
hi, i got problem with my code and hopefully someone able to figure it out. The main purpose is to sort array based on its value (then reindex its numerical key).
i got this sample of filename :
$filename = array("index 198.php", "index 192.php", "index 144.php", "index 2.php", "index 1.php", "index 100.php", "index 111.php");
$alloutput = array(); //all of index in array
foreach ($filename as $name) {
preg_match('#(\d+)#', $name, $output); // take only the numerical from file name
array_shift($output); // cleaned. the last code create duplicate numerical in $output,
if (is_array($hasilku)) {
$alloutput = array_merge($alloutput, $output);
}
}
//try to check the type of every value in array
foreach ($alloutput as $output) {
if (is_array($hasil)) {
echo "array true </br>";
} elseif (is_int($hasil)) {
echo "integer true </br>";
} elseif (is_string($hasil)) { //the numerical taken from filename always resuld "string".
echo "string true </br>";
}
}
the output of this code will be :
Array ( [0] => 198 [1] => 192 [2] => 144 [3] => 2 [4] => 1 [5] => 100 [6] => 111 )
i have test every output in array. It's all string (and not numerical), So the question is how to change this string to integer, so i can sort it from the lowest into the highest number ?
the main purpose of this code is how to output array where it had been sort from lowest to highest ?
© Stack Overflow or respective owner