Sorting an array in different ways with efficient output I am going through some PHP array exercises on w3resource because I want to get to grips with all the different functions PHP offers for arrays. I have just done this one:


  
  Write a PHP script to sort the following associative array : array("Sophia"=&gt;"31","Jacob"=&gt;"41","William"=&gt;"39","Ramesh"=&gt;"40") in
  
  
  a) ascending order sort by value
  
  b) ascending order sort by Key
  
  c) descending order sorting by Value
  
  d) descending order sorting by Key


And here is my code:

&lt;?php
    echo "&lt;pre&gt;";

    $people = array("Sophia"=&gt;"31","Jacob"=&gt;"41","William"=&gt;"39","Ramesh"=&gt;"40");

    function display($array)
    {
        foreach($array as $p =&gt; $a)
        {
        echo "$p\t $a&lt;br&gt;";
        }
    }

    //sorted by value, ascending
    asort($people);
    display($people);
    echo"&lt;br&gt;";

    //sorted by key, ascending
    ksort($people);
    display($people);
    echo"&lt;br&gt;";

    //sorted by value, descending
    arsort($people);
    display($people);
    echo"&lt;br&gt;";

    //sorted by key, descending
    krsort($people);
    display($people);

    echo "&lt;/pre&gt;";
?&gt;


I was pretty content with how I solved this, using the display() helper method to cut down on typing. 

However, the example on the website is as follows:

&lt;?php
echo "
Associative array : Ascending order sort by value
";
$array2=array("Sophia"=&gt;"31","Jacob"=&gt;"41","William"=&gt;"39","Ramesh"=&gt;"40"); asort($array2);
foreach($array2 as $y=&gt;$y_value)
{
echo "Age of ".$y." is : ".$y_value."
";
}
echo "
Associative array : Ascending order sort by Key
";
$array3=array("Sophia"=&gt;"31","Jacob"=&gt;"41","William"=&gt;"39","Ramesh"=&gt;"40"); ksort($array3);
foreach($array3 as $y=&gt;$y_value)
{
echo
"Age of ".$y." is : ".$y_value."
";
}
echo "
Associative array : Descending order sorting by Value
";
$age=array("Sophia"=&gt;"31","Jacob"=&gt;"41","William"=&gt;"39","Ramesh"=&gt;"40");
arsort($age);
foreach($age as $y=&gt;$y_value)
{
echo "Age of ".$y." is : ".$y_value."
";
}
echo "
Associative array : Descending order sorting by Key
";
$array4=array("Sophia"=&gt;"31","Jacob"=&gt;"41","William"=&gt;"39","Ramesh"=&gt;"40"); krsort($array4);
foreach($array4 as $y=&gt;$y_value)
{
echo "Age of ".$y." is : ".$y_value."
";
} 
?&gt;


This came across, to me, as flawed code in some ways, because I wondered why the same array was declared multiple times when it seems unnecessary.


Am I missing something with this example code? Is there a reason I should not use a display() helper method?
Is readability improved by following echo immediately with a line, as is done in the example?
Is it good practise to use the . concatenation operator alongside ' ' (single quotation marks), as opposed to simply putting everything inside " " (doubles)? (I find the latter more readable and easier to work with.)
Is it acceptable to place more than one statement per line, as is done in the example

$array2=array("Sophia"=&gt;"31","Jacob"=&gt;"41","William"=&gt;"39","Ramesh"=&gt;"40"); asort($array2);
There isn't much to say about your code, it's pretty good. You avoided duplication by extracting code to functions, and your code is pretty clean in general.

Some nitpicks:


echo"&lt;br&gt;";: you are missing a space here
your array would also be more readable with an extra space after , and spaces around =&gt;. Putting each array value on it's own line will increase readability even more (the extra space it takes up is well worth it).
your echo needs to be indented a bit more.
$p and $a could be $person and $age for more clarity.


Reviewing code not written by you is off-topic, so I'll be brief about that. But yes, you are right with all your questions. There is a lot of duplication in that code (the duplicate arrays might be explained by them wanting to sort the exact same array each time, instead of one that has already been changed by another sort function; in that case, a getPerson function would avoid duplication), the formatting is pretty bad (unnecessary newlines, no paragraphs, no indentation, lack of spaces, etc), multiple statements on one line, and so on.