How does asort work?

I looked through some of my old works and saw that the asart function would work perfectly for some of the applications that I need, although I tried to make a completely different and lanky path.

So my question is, how does asort support association when sorting? I thought that the array can be sorted by key or by value, is there a third sorting bar?

+3
source share
2 answers

The "third core" is the actual location in the memory / array.
You will see this clearly when doing foreach on the following two arrays, which are the same but have a different order:

$x1=array('mmm'=>'mmm','bbb'=>'bbb','ccc'=>'ccc');
$x2=array('ccc'=>'ccc','bbb'=>'bbb','mmm'=>'mmm');

foreach($x1 as $k=>$v) echo "{$k} {$v}";
foreach($x2 as $k=>$v) echo "{$k} {$v}";

doing asort by default for these two arrays will lead to both cases in:

$x1=array('bbb'=>'bbb','ccc'=>'ccc','mmm'=>'mmm');
$x2=array('bbb'=>'bbb','ccc'=>'ccc','mmm'=>'mmm');
+1

:

asort -

, :

  • Asort , index = > value.
  • Arsort - , -.

.

, :

  • - >
  • Asc Desc
+1

Source: https://habr.com/ru/post/1795128/


All Articles