|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-05-20 15:01 UTC] swoo at gvlabs dot com
<?php
class test
{
var $items = array();
function stuff($listx){
for($i=0;$i<count($listx);$i++){
array_push($this->items,$listx[$i]);
}
}
function testsort1(){
echo "<hr>sort<hr>";
sort($this->items);
for($i=0;$i<count($this->items);$i++){
echo $this->items[$i]."<br>";
}
}
function testsort2(){
echo "<hr>natsort<hr>";
natsort($this->items);
for($i=0;$i<count($this->items);$i++){
echo $this->items[$i]."<br>";
}
}
function testsort3(){
echo "<hr>natcasesort<hr>";
natcasesort($this->items);
for($i=0;$i<count($this->items);$i++){
echo $this->items[$i]."<br>";
}
}
}
$t = array("apple","car","zebra","Foobar","Blank");
$K = new test;
$K->stuff($t);
$K->testsort1();
$J = new test;
$J->stuff($t);
$J->testsort2();
$M = new test;
$M->stuff($t);
$M->testsort3();
echo "<hr>";
natcasesort($t);
foreach($t as $t2){
echo $t2."<br>";
}
?>
used the php binary for win32
natcasesort works outside of the class object as demonstated by the last example.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 09:00:01 2025 UTC |
natcasesort won't mess with the keys.. so you can have an array like this $a['a'] = "apple"; $a['c'] = "car"; $a['z'] = "zebra"; $a['f'] = "Foobar"; $a['b'] = "Blank"; natcasesort($a); and you still can say echo $a['a'] and get "apple". now your array is indexed by integers so it still will keep the interger index. to fix this you should use foreach() both in the class and outside the class (which you did) not this: for($i=0;$i<count($this->items);$i++){ echo $this->items[$i]."<br>"; } but this: foreach($this->items as $item) echo $item . "<br>";