2

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($output)) {
        $alloutput = array_merge($alloutput, $output);
    }
  }


//try to check the type of every value in array
foreach ($alloutput as $output) { 
    if (is_array($output)) {
        echo "array true </br>";        
    } elseif (is_int($output)) {
        echo "integer true </br>";
    } elseif (is_string($output)) {   //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 ?

2
  • I don't understand your code, there are undefined vars in your loops (hasil and hasilku) Commented Mar 27, 2010 at 9:54
  • nicolo, i'm very sorry with that. i will try to change it Commented Mar 27, 2010 at 10:26

2 Answers 2

1

preg_match will keep the matched part in $outpu[1], so you can make use of that to convert the string to int and then add it to your alloutput array.

foreach ($filename as $name) {
    preg_match('#(\d+)#', $name, $output);
    $alloutput[] = intval($output[1]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

it's you again...lol Thanks i will try to find the way to sort it ;D
sorry i use the wrong code in the above code. but i understand your answer ;D
@justjoe: To sort you can use the sort function. Also you need not have to convert the strings to int before you sort. The sort function takes an argument SORT_NUMERIC. Which will treat your array elements are numeric. Give it a try.
0

Use intval

$number_string = '14';
$number = intval('14');

With intval you can specify also the basis. If the number is decimal, hower, you can also use

$number = (int) $number_string;

1 Comment

i know about this. but i hope the solution given in the first foreach. so it will be more directly.sorry if it's too much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.