There is an array:

$array[0]= '5'; $array[1]= '3'; $array[2]= '2'; $array[3]= '1'; $array[4]= '14'; 

How to sort its values ​​into something like this:

 $array[0]= '1'; $array[1]= '2'; $array[2]= '3'; $array[3]= '5'; $array[4]= '14'; 
  • one
    I vote for the closure of this issue as not relevant topic, because rtfm - Alexey Shimansky
  • @PinkTux never understood such personalities :) it's easier for them to register on the site and write the whole question, instead of writing in Google sort array php and in the first link see comprehensive information on this issue: php.net/manual/ru/ array.sorting.php - Crystal
  • 2
    I suggest that you read, for example, the discussion of all outraged (and minus answers): The resource from the knowledge base for specialists turns into a consultation center for newbies - aleksandr barakin
  • I truly understand that the lines are sorted here by their numerical value? - Kromster
  • @ Alexey Shimansky having an answer in manuals does not affect the compliance with the topic. - etki

2 answers 2

In your case, the function will help

 sort($array); 

http://php.net/manual/ru/function.sort.php

As the first argument, this function takes an array and sorts it. The new array will be written instead of the old one (here the argument is passed by reference).

  • And the fact that the question should be sorted by numeric values, your answer takes into account? - Kromster

Here is:

 $array= array("1", "2", "3", "4"); sort($array, SORT_NATURAL | SORT_FLAG_CASE); foreach ($array as $key => $val) { echo "array[" . $key . "] = " . $val . "\n"; } 
  • 2
    add a description of why these flags are needed - Grundy