note 86848 modified in function.array-unique by danbrown
| From: | danbrown@php.net | Date: | Thu, 06 Nov 2008 14:50:00 +0000 |
| Subject: | note 86848 modified in function.array-unique by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146214@lists.php.net to get a copy of this message | ||
so .... my problem was multidimensional sort.
<?php
$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"]); }
}
?>
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:
<?php
/* 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
--was--
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
http://php.net/manual/en/function.array-unique.php