Jump to content

Array sort example


Muck A. Round
 Share

Recommended Posts

I feel like I'm missing something on the example for Sort Array (Ascending Order), According to Value - asort().    I added a var_dump and also switched the variables to ABC, and don't see that any sorting is occurring.

<!DOCTYPE html>
<html>
<body>

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);

var_dump($age);
echo "<br><br>";


foreach($age as $x => $x_value) {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
  }

echo "<br>";
  
$age = array("C"=>"35", "B"=>"37", "A"=>"43");
asort($age);

var_dump($age);
echo "<br>";
echo "<br>";

foreach($age as $x => $x_value) {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
}
?>

</body>
</html>

Edited by Muck A. Round
changed quotes to bold face
Link to comment
Share on other sites

Ah - thanks everyone.  What I was missing was that an associative array is defined as KEY => VALUE.  So when you say "value" you're not talking about the value (or amount?) of any given entry, but value as the entity that each key is associated with. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...