You are here: Re: Sort array without index « PHP Programming Language « IT news, forums, messages
Re: Sort array without index

Posted by F Laupretre on 02/08/06 00:13

Le Tue, 07 Feb 2006 21:10:57 +0100, <christiang@lateral.net> a écrit:

> Actually, I tried the sorting outside my class and it works properly:
....
> usort($arr, "cmp");


If you want to keep the key associations, you must use uasort() instead of
usort().

> I have problem on modifying the array of my class, I tried even to
> remove the elements but I can't:
>
> foreach($this->relatedtags_arr as $relatedtag){
> unset($this->relatedtags_arr[$relatedtag['name_str']]);

Why do you replicate your key in the value ? Seems very complex without
need. You can keep only the key and sort on it, event with a user compare
function (with uksort). But maybe the best solution would be the opposite
method : removing the string keys and keeping the value field...

You can compare on any field with the following code inside your class :

var $field;
var $a; // Array

function sort_array($field)
{
$this->field=$field;
uasort($a,$a,$this->cmp);
}

function cmp($a1,$a2)
{
return strcmp($a1[$this->field],$a2[$this->field]);
}
//-- End of class

And you sort your array with something like :

$object->sort_array('field_name');

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация