note 86848 added to function.array-unique
| From: | jusvalceanu-SPAM at SPAM-yahoo dot com | Date: | Thu, 06 Nov 2008 10:23:04 +0000 |
| Subject: | note 86848 added to function.array-unique | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146202@lists.php.net to get a copy of this message | ||
so .... my problem was:
multidimensional sort
<?
$new = array();
$exclude = array("");
for ($i = 0; $i<=count($attribs)-1; $i++) {
if (!in_array(trim($attribs[$i]["price"]) ,$exclude)) { $new[] = $attribs[$i];
$exclude[] = trim($attribs[$i]["price"]); }
}
?>
so did you figure`it out or do you need more explications ?
Explication
Array $attribs is an array contaning arrays. Each array in the $attrib array consists in multiple
fields (ex: name, lenght, price, etc.) to be more simpler in speech think that $attrib is the array
resulted by a search sql query done by a visitator on your online shoopping website ... (so ... each
array in the $attrib is a product :P) if you want to sort only the uniq results use the above or use
this:
<?
/* Our Array of products */
$attribs[] = array(
"name" => "Test Product 1",
"length" => "42 cm",
"weight" => "0,5 kg",
"price" => "10 $",
"stock" => "100",
);
$attribs[] = array(
"name" => "Test Product 2",
"length" => "42 cm",
"weight" => "1,5 kg",
"price" => "10 $",
"stock" => "200",
);
/* The nice stuff */
$new = array();
$exclude = array("");
for ($i = 0; $i<=count($attribs)-1; $i++) {
if (!in_array(trim($attribs[$i]["price"]) ,$exclude)) { $new[] = $attribs[$i];
$exclude[] = trim($attribs[$i]["price"]); }
}
print_r($new); // $new is our sorted array
?>
Have fun tweaking this ;)) i know you will ;))
From Romania With Love
----
Server IP: 69.147.83.197
Probable Submitter: 92.84.108.73
----
Manual Page -- http://www.php.net/manual/en/function.array-unique.php
Edit -- https://master.php.net/note/edit/86848
Del: integrated -- https://master.php.net/note/delete/86848/integrated
Del: useless -- https://master.php.net/note/delete/86848/useless
Del: bad code -- https://master.php.net/note/delete/86848/bad+code
Del: spam -- https://master.php.net/note/delete/86848/spam
Del: non-english -- https://master.php.net/note/delete/86848/non-english
Del: in docs -- https://master.php.net/note/delete/86848/in+docs
Del: other reasons-- https://master.php.net/note/delete/86848
Reject -- https://master.php.net/note/reject/86848
Search -- https://master.php.net/manage/user-notes.php