note 33419 modified in function.array-multisort by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 13:59:10 +0000 |
| Subject: | note 33419 modified in function.array-multisort by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-80083@lists.php.net to get a copy of this message | ||
How to order a multidimensional array without writing each key of its.
I had a multi-array like this:
<?php
$mega-array = array('sub-arr1' =>
array(
'url' => 'http://www.e-allora.net/ompeig.php',
'point1' => '20',
'point2' => '11',
'point3' => '3'),
'sub-arr2' =>
array(
'url' => 'http://www.fixproject.org',
'point1' => '5',
'point2' => '3',
'point3' => '7');
?>
...and I want to order its elements (links in a mainpage) by different point-value (more point
before, less point after) in different menues (one for "point1", another one for
"point3", another one for "point2").
I wrote this function:
<?php
function byPoint($array,$param)
# where the value of $param could be point(1|2|3), but you can name with
# other names your categories
# byPoint($array,"point1");
{
while($var1 = each($array))
{
$actual_key = $var1['key'];
$actual_value = $array[$actual_key][$param];
$array_1[$actual_key] = $actual_value;
}
reset($array);
arsort($array_1);
$x = 0;
while($new_value = each($array_1))
{
$new_key = $new_value['key'];
$url = $array[$new_key]['url'];
$x++;
if($x < 6)
{
echo "<a href=\"$url\" class=\"main\">";
echo "$chiave</a>·\n";
}
}
reset($array);
}
?>
Shar&njoy
--was--
How to order a multidimensional array without writing each key of its.
I had a multi-array like this:
$mega-array = array('sub-arr1' =>
array(
'url' => 'http://www.e-allora.net/ompeig.php',
'point1' => '20',
'point2' => '11',
'point3' => '3'),
'sub-arr2' =>
array(
'url' => 'http://www.fixproject.org',
'point1' => '5',
'point2' => '3',
'point3' => '7');
...and I want to order its elements (links in a mainpage) by different point-value (more point
before, less point after) in different menues (one for "point1", another one for
"point3", another one for "point2").
I wrote this function:
function byPoint($array,$param)
# where the value of $param could be point(1|2|3), but you can name with
# other names your categories
# byPoint($array,"point1");
{
while($var1 = each($array))
{
$actual_key = $var1['key'];
$actual_value = $array[$actual_key][$param];
$array_1[$actual_key] = $actual_value;
}
reset($array);
arsort($array_1);
$x = 0;
while($new_value = each($array_1))
{
$new_key = $new_value['key'];
$url = $array[$new_key]['url'];
$x++;
if($x < 6)
{
echo "<a href=\"$url\" class=\"main\">";
echo "$chiave</a>·\n";
}
}
reset($array);
}
Shar&njoy
http://php.net/manual/en/function.array-multisort.php