Witam
Mam w tablicy $table informacje o plikach w formie $table[IdPliku'][NazwaWlasciwosci], czyli tak:
// $table [ IdPliku ] [ NazwaWlasciwosci ]
$table[0]['name'] = 'Code.php'; //1
$table[0]['size'] = 2142; //3
$table[0]['date'] = 1456589; //3
$table[1]['name'] = 'clearCode.php'; //2
$table[1]['size'] = 654; //2
$table[1]['date'] = 1456234; //2
$table[2]['name'] = 'index.php'; //4
$table[2]['size'] = 56; //1
$table[2]['date'] = 1456912; //4
$table[3]['name'] = 'functions.php'; //3
$table[3]['size'] = 65142; //4
$table[3]['date'] = 1456134; //1
chce to sortowac wedlug wybranej wlasciwosci - po nazwie, po rozmiarze i po dacie
probowalem roznych funkcji, najbardziej sensowna wydaje sie ta z przykladu w manualu, ale tez nie do konca
uzyta funkcja:
function cmp($a, $b)
{
return strcmp($a["name"], $b["name"]); }
// $table [ IdPliku ] [ NazwaWlasciwosci ]
$table[0]['name'] = 'Code.php'; //1
$table[0]['size'] = 2142; //3
$table[0]['date'] = 1456589; //3
$table[1]['name'] = 'clearCode.php'; //2
$table[1]['size'] = 654; //2
$table[1]['date'] = 1456234; //2
$table[2]['name'] = 'index.php'; //4
$table[2]['size'] = 56; //1
$table[2]['date'] = 1456912; //4
$table[3]['name'] = 'functions.php'; //3
$table[3]['size'] = 65142; //4
$table[3]['date'] = 1456134; //1
for($x=0;$table[$x]['size'];$x++)
{
echo $table[$x]['size'].'<br/>'; }
a tym przypadku dziala idealnie, ale jak zrobie sortowanie wedlug rozmiaru, to sortuje alfabetycznie, a nie wedlug wielkosci
wyszukalem, ze do sortowanie alfanumerycznego sluzy natsort, ale ta funkcja nie ma swojego odpowiednika do tablic wielowymiarowych
Probowalem jeszcze zrobic cos takiego:
function cmp($a, $b)
{
if($a["size"]< $b["size"])
{
return -4;
}
}
ale to nie dziala
Jesli nie ma sposobu, to mam pomysl na napisanie wlasnego skryptu, ale strasznie skomplikowany mi sie wydaje.
EDIT:
dla
function cmp($a, $b)
{
if($a["size"]< $b["size"])
{
return 5;
}
}
dziala, choc z opisu strcmp wynika inaczej
EDIT:
napisalem calosc
$sortType = 'size';
function cmp($a, $b)
{
if($sortType == 'name')
{
return strcmp($a["name"], $b["name"]); }
elseif($sortType == 'size')
{
if($a["size"]> $b["size"])
{
return 5;
}
}
elseif($sortType == 'date')
{
if($a["date"]> $b["date"])
{
return 5;
}
}
}
class ff
{
// $table [ IdPliku ] [ NazwaWlasciwosci ]
function __construct()
{
$this->table[0]['name'] = 'Code.php'; //1
$this->table[0]['size'] = 2142; //3
$this->table[0]['date'] = 1456589; //3
$this->table[1]['name'] = 'clearCode.php'; //2
$this->table[1]['size'] = 654; //2
$this->table[1]['date'] = 1456234; //2
$this->table[2]['name'] = 'index.php'; //4
$this->table[2]['size'] = 56; //1
$this->table[2]['date'] = 1456912; //4
$this->table[3]['name'] = 'functions.php'; //3
$this->table[3]['size'] = 65142; //4
$this->table[3]['date'] = 1456134; //1
}
{
usort($this->table, "cmp");
for($x=0;$this->table[$x]['name'];$x++)
{
echo $this->table[$x]['name'].'<br/>'; echo $this->table[$x]['size'].'<br/>'; echo $this->table[$x]['date'].'<br/><br/>'; }
}
}
$f= new ff();
$f->sort();
wiec raczej temat jest do zamkniecia...
Ten post edytował Kamil Pietrzak 16.10.2010, 00:29:17