Search Results

Search found 21 results on 1 pages for 'usort'.

Page 1/1 | 1 

  • Rewriting usort function because of fatal error (PHP bug)

    - by Lionel
    The two following usort functions throw fatal error Base lambda function for closure not found in our productive environment (PHP 5.4). This seems to be a known PHP bug that should be fixed by now (https://bugs.php.net/bug.php?id=52144), but it still occurs for us. Anyway, we unfortunately don't have time to figure out what's wrong with our PHP configurations etc. We would like to rewrite these two functions without the use of anonymous functions, so that the error doesn't occur anymore. Ordering of a multidimensional array ($array) by value of key "position": usort($array, function($a, $b) { return $a['position'] - $b['position']; }); Ordering of a multidimensional array ($array) according to the order of a second array ($position_order): usort($array, function($a, $b) use($position_order) { return (isset($position_order[$a['ftid']]) ? ($position_order[$a['ftid']] - $position_order[$b['ftid']]) : 1); }); Especially the latter causes some headache, as we don't know how to pass the "outside" array $position_order.

    Read the article

  • usort - more parameters

    - by pgfonline
    Hallo, how can I pass more parameters to usort? I have different functions, which are very similar in structure, I want to have just one function: <?php $arr = array( array('number' => 100, 'string'=>'aaa'), array('number'=>50, 'string'=>'bdef'), array('number'=>150, 'string'=>'cbba') ); usort($arr, 'sortNumberDesc'); //How can I use just a single function? //How can I pass further parameters to usort? function sortNumberDesc($a, $b){ $a = $a['number']; $b = $b['number']; if ($a == $b) return 0; return ($a > $b) ? -1 : +1; } function sortNumberAsc($a, $b){ $a = $a['number']; $b = $b['number']; if ($a == $b) return 0; return ($a < $b) ? -1 : +1; } //I want to do the same with just one function: //Sort ID is the search index, reverse DESC or ASC function sort($a, $b, $sortId='number', $reverse = 0){ $a = $a[$sortId]; $b = $b[$sortId]; if ($a == $b) return 0; if($reverse == false) return ($a > $b) ? -1 : +1; else return ($a < $b) ? -1 : +1; } print_r($arr); ?>

    Read the article

  • second sorting with php usort

    - by bluedaniel
    So Ive got a pretty big array of data and need to sort them by two criteria. There is variable $data['important'] and $data['basic']. They are simple numbers and I am using uasort to sort $data firstly by important and then by basic. So Important | Basic 10 | 8 9 | 9 9 | 7 7 | 9 The usort function is a simple public function sort_by_important($a, $b) { if ($a[important] > $b[important]) { return -1; } elseif ($b[important] > $a[important]) { return 1; } else { return 0; } } How can I re-sort the array to the second variable and keep the Important ordering? Thanks everyone. EDIT How about adding a third sorting option after this even? So Important Basic Less

    Read the article

  • using usort to re-arrange multi-dimension array

    - by Thomas Bennett
    I have a large array: [0] => stdClass Object ( [products] => Array ( [0] => stdClass Object ( [body_html] => bodyhtml [published_at] => 2012-12-16T23:59:18+00:00 [created_at] => 2012-12-16T11:30:24+00:00 [updated_at] => 2012-12-18T10:52:14+00:00 [vendor] => Name [product_type] => type ) [1] => stdClass Object ( [body_html] => bodyhtml [published_at] => 2012-12-16T23:59:18+00:00 [created_at] => 2012-12-16T10:30:24+00:00 [updated_at] => 2012-12-18T10:52:14+00:00 [vendor] => Name [product_type] => type ) ) ) and I need to arrange each of the products by the date they where created... I've tried and failed all kinds of usort, ksort, uksort techniques to try and get it to be in a specific order (chronological) but failed! any guidance would be most appreciated

    Read the article

  • How perform USort() on an Array of Objects class definition as a method?

    - by user558724
    class Contact{ public $name; public $bgcolor; public $lgcolor; public $email; public $element; public function __construct($name, $bgcolor, $lgcolor, $email, $element) { $this->name = $name; $this->bgcolor = $bgcolor; $this->lgcolor = $lgcolor; $this->email = $email; $this->element = $element; } public static function sortByName(Contact $p1, Contact $p2) { return strcmp($p1->name, $p2->name); } } class ContactList implements Iterator, ArrayAccess { protected $_label; protected $_contacts = array(); public function __construct($label) { $this->_label = $label; } public function getLabel() { return $this->_label; } public function addContact(Contact $contact) { $this->_contacts[] = $contact; } public function current() { return current($this->_contacts); } public function key() { return key($this->_contacts); } public function next() { return next($this->_contacts); } public function rewind() { return reset($this->_contacts); } public function valid() { return current($this->_contacts); } public function offsetGet($offset) { return $this->_contacts[$offset]; } public function offsetSet($offset, $data) { if (!$data instanceof Contact) throw new InvalidArgumentException('Only Contact objects allowed in a ContactList'); if ($offset == '') { $this->_contacts[] = $data; } else { $this->_contacts[$offset] = $data; } } public function offsetUnset($offset) { unset($this->_contacts[$offset]); } public function offsetExists($offset) { return isset($this->_contacts[$offset]); } public function sort($attribute = 'name') { $sortFct = 'sortBy' . ucfirst(strtolower($attribute)); if (!in_array($sortFct, get_class_methods('Contact'))) { throw new Exception('contact->sort(): Can\'t sort by ' . $attribute); } usort($this->contact, 'ContactList::' . $sortFct); } } public function Sort($property, $asc=true) { // this is where sorting logic takes place $_pd = $this->_contact->getProperty($property); if ($_pd == null) { user_error('Property '.$property.' does not exist in class '.$this->_contact->getName(), E_WARNING); return; } // set sortDescriptor ContactList::$sortProperty = $_pd; // and apply sorting usort($this->_array, array('ContactList', ($asc?'USortAsc':'USortDesc'))); } function getItems(){ return $this->_array; } class SortableItem extends ContactList { static public $sortProperty; static function USortAsc($a, $b) { /*@var $_pd ReflectionProperty*/ /* $_pd = self::$sortProperty; if ($_pd !== null) { if ($_pd->getValue($a) === $_pd->getValue($b)) return 0; else return (($_pd->getValue($a) < $_pd->getValue($b))?-1:1); } return 0; } static function USortDesc($a, $b) { return -(self::USortAsc($a,$b)); } } This approach keeps giving me PHP Warnings: usort() [function.usort]: of all kinds which I can provide later as needed to comment out those methods and definitions in order to test and fix some minor bugs of our program. **$billy parameters are already defined. $all -> addContact($billy); // --> ended up adding each contact manually above $all->Sort('name',true); $items = $all->getItems(); foreach($items as $contact) { echo $contact->__toString(); } $all->sort(); The reason for using usort is to re-arrange the order alphabetically by name but somehow is either stating that the function comparison needs to be an array or another errors which obviously I have seemed to pass. Any help would be greatly appreciated, thanks in advance.

    Read the article

  • php - usort or array_multisort?

    - by Simpson88Keys
    Trying to sort the array below by memnum in ascending order, and I'm a bit confused which is better to use... usort or array_multisort? I was thinking usort because it's multidimensional? Does anyone have an example of this? Array ( [0] => Array ( [memnum] => 3236467423 [mid] => 1104881300 [fname] => JOHN [lname] => DOE [add1] => OMITTED [add2] => [city] => CHESTERFIELD [state] => MI [zip] => 48051 [age] => 50 ) [1] => Array ( [memnum] => 3258467922 [mid] => 1105121457 [fname] => JANE [lname] => DOE [add1] => OMITTED [add2] => [city] => CHESTERFIELD [state] => MI [zip] => 48051 [age] => 50 ) [2] => Array ( [memnum] => 3237769108 [mid] => 1104489312 [fname] => BOB [lname] => DOE [add1] => OMITTED [add2] => [city] => CHESTERFIELD [state] => MI [zip] => 48051 [age] => 50 ) )

    Read the article

  • PHP anonymous functions scope question

    - by Dan
    Hi, I'm trying to sort an array of objects by a common property, however I cannot get my $property parameter to register in the inner function (I can use it in the outer one OK). The way I read the documentation, it sounded like the parameter would be available, have I misunderstood something? Here is what I have: public static function sortObjectsByProperty($objects, $property) { function compare_object($a, $b) { $a = $a->$property; $b = $b->$property; if ($a->$property == $b->$property) { return 0; } return ($a->$property > $b->$property) ? +1 : -1; } usort($objects, 'compare_object'); return $objects; } Any advice appreciated. Thanks.

    Read the article

  • PHP search multidimensional array for value, then place that element at start of array

    - by BobFlemming
    I need to search this array: cars - [0] -make : Ford -model: Escort -year: 1991 [1] -make: Honda -model: Civic -year: 1996 [2] -make: Vauxhall -model: Astra -year: 1972 And if (for example) the model is "Civic" , place that 'car' at position 0. So the end array would be like: cars - [0] -make: Honda -model: Civic -year: 1996 [1] -make : Ford -model: Escort -year: 1991 [2] -make: Vauxhall -model: Astra -year: 1972 I've tried some usort variations: function typeSort($a, $b) { if ($a['model'] == 'Civic' ) { return 0; } return ($a['model'] < $b['model']) ? -1 : 1; } but this is just returning 1

    Read the article

  • php | Multidimensional array sorting

    - by user889349
    I have an array and need to be sorted (based on id): Array ( [0] => Array ( [qty] => 1 [id] => 3 [name] => Name1 [sku] => Model 1 [options] => [price] => 100.00 ) [1] => Array ( [qty] => 2 [id] => 1 [name] => Name2 [sku] => Model 1 [options] => Color: <em>Black (+10$)</em>. Memory: <em>32GB (+99$)</em>. [price] => 209.00 ) ) Is it possible to sort my array to get output (id based)? Array ( [0] => Array ( [qty] => 2 [id] => 1 [name] => Name2 [sku] => Model 1 [options] => Color: <em>Black (+10$)</em>. Memory: <em>32GB (+99$)</em>. [price] => 209.00 ) [1] => Array ( [qty] => 1 [id] => 3 [name] => Name1 [sku] => Model 1 [options] => [price] => 100.00 ) ) Thanks!

    Read the article

  • Custom array sorting based on instance properties

    - by St. John Johnson
    I'm trying to perform a usort on an array inside an instance of a class. But the sort is dependent on the properties of said instance. Code (which doesn't work): class foo { private $array; private $key; private $dir; function sort() { usort($this->array, array("foo", "orderArray")); } function orderArray($a, $b) { return strcmp($a[$this->key], $b[$this->key]) * $this->dir; } } From the orderArray class, you can't access $key or $dir. The question is, how can I write this so I can?

    Read the article

  • multi-dimension array value sorting in php

    - by David
    Another potentially interesting (or n00b, whatever comes first) question. I am building up an array with a set of database fields with information about table, actual field name and descriptive field name as a multi-dimensional array. Here is what it currently looks like: $Fields['User']['ID'] = "User ID"; $Fields['User']['FirstName'] = "First Name"; $Fields['Stats']['FavouriteOrder'] = "Favourite Item Ordered"; $Fields['Geographic']['Location'] = "Current Location"; $Fields['Geographic']['LocationCode'] = "Current Location Code"; Okay, this is fine, but I am piping this into a system that allows exporting of selected fields, and in the end I want to foreach() through the different levels, extract the data and then ultimately have all the descriptive fields to be displayed sorted alphabetically using their descriptive name. So ultimately in the order: Current Location, Current Location Code, Favourite Item Ordered, First Name then User ID - obviously keeping index associations. I can't use usort() and I can't use array_multisort()... or maybe I can and I just don't know how. usort() seems to need a key to sort by, but I have variable keys. array_multisort() just seems to do the same as sort() really. Any ideas?

    Read the article

  • PHP sorting issue with simpleXML

    - by tugbucket
    test.xml: <?xml version="1.0"?> <props> <prop> <state statename="Mississippi"> <info> <code>a1</code> <location>Jackson</location> </info> <info> <code>d2</code> <location>Gulfport</location> </info> <info> <code>g6</code> <location>Hattiesburg</location> </info> </state> <state statename="Texas"> <info> <code>i9</code> <location>Dallas</location> </info> <info> <code>a7</code> <location>Austin</location> </info> </state> <state statename="Maryland"> <info> <code>s5</code> <location>Mount Laurel</location> </info> <info> <code>f0</code> <location>Baltimore</location> </info> <info> <code>h4</code> <location>Annapolis</location> </info> </state> </prop> </props> test.php // start the sortCities function sortCities($a, $b){ return strcmp($a->location, $b->location); } // start the sortStates function sortStates($t1, $t2) { return strcmp($t1['statename'], $t2['statename']); } $props = simplexml_load_file('test.xml'); foreach ($props->prop as $prop) { $sortedStates = array(); foreach($prop->state as $states) { $sortedStates[] = $states; } usort($sortedStates, "sortStates"); // finish the sortStates /* --- */ echo '<pre>'."\n"; print_r($sortedStates); echo '</pre>'."\n"; /* --- */ foreach ($prop->children() as $stateattr) { // this doesn't do it //foreach($sortedStates as $hotel => @attributes){ // blargh! if(isset($stateattr->info)) { $statearr = $stateattr->attributes(); echo '<optgroup label="'.$statearr['statename'].'">'."\n"; $options = array(); foreach($stateattr->info as $info) { $options[] = $info; } usort($options, "sortCities"); // finish the sortCities foreach($options as $stateattr => $info){ echo '<option value="'.$info->code.'">'.$info->location.'</option>'."\n"; } echo '</optgroup>'."\n"; } else { //empty nodes don't do squat } } } ?> This is the array that: print_r($sortedStates); prints out: Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [statename] => Maryland ) [info] => Array ( [0] => SimpleXMLElement Object ( [code] => s5 [location] => Mount Laurel ) [1] => SimpleXMLElement Object ( [code] => f0 [location] => Baltimore ) [2] => SimpleXMLElement Object ( [code] => h4 [location] => Annapolis ) ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [statename] => Mississippi ) [info] => Array ( [0] => SimpleXMLElement Object ( [code] => a1 [location] => Jackson ) [1] => SimpleXMLElement Object ( [code] => d2 [location] => Gulfport ) [2] => SimpleXMLElement Object ( [code] => g6 [location] => Hattiesburg ) ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [statename] => Texas ) [info] => Array ( [0] => SimpleXMLElement Object ( [code] => i9 [location] => Dallas ) [1] => SimpleXMLElement Object ( [code] => a7 [location] => Austin ) ) ) ) this: // start the sortCities function sortCities($a, $b){ return strcmp($a->location, $b->location); } plus this part of code: $options = array(); foreach($stateattr->info as $info) { $options[] = $info; } usort($options, "sortCities"); // finish the sortCities foreach($options as $stateattr => $info){ echo '<option value="'.$info->code.'">'.$info->location.'</option>'."\n"; } is doing a fine job of sorting by the 'location' node within each optgroup. You can see that in the array I can make it sort by the attribute 'statename'. What I am having trouble with is echoing out and combining the two functions in order to have it auto sort both the states and the cities within and forming the needed optgroups. I tried copying the lines for the cities and changing the names called several ways to no avail.

    Read the article

  • Order Multidimensional Arrays PHP

    - by ronsandova
    Hi everyone I have some problem to order an array by a field of this, here i leave the example foreach($xml as $site){ echo '<div><a href="'.$site->loc.'">'.$site->loc.'</a>' .$site->padre.'</div>'; } Some times the filed $site->padre is empty but i'd like to order by $site->padre alphabetical i saw example with usort but i don't understand how to work it. Thanks in advance. Cheers

    Read the article

  • sorting a list of objects by value php

    - by Mike
    I have a list of objects: class Beer { var $name; var $id; var $style; var $brewery; var $rate; function getName() { return $this->name; } function getID() { return $this->id; } function getStyle() { return $this->style; } function getBrewery() { return $this->brewery; } function getRate() { return $this->rate; } } After doing some research online on how to accomplish this, this is my code: usort($localBeersList, "cmp"); function cmp($a, $b) { if ($a->getRate() == $b->getRate()) { return 0; } return ($a->getRate() < $b->getRate()) ? -1 : 1; } If I try and output my list after this I do not get anything.

    Read the article

  • PHP sorting an array by mysql date.

    - by daviemanchester
    Hi I have an array that I would like to sort using a date field from a mysql database. Here is a sample of the array which is named 'news' in my class: [48] => Array ( [id] => 14 [type] => 3 [updated] => 2010-04-17 13:54:42 ) [49] => Array ( [id] => 15 [type] => 3 [updated] => 2010-04-17 13:57:21 ) I want to sort de by the 'updated' field. I have some code I have started but am unsure how to complete it and get it working. function sortNews($x) { usort($this->news, array("ProcessClass", "cmp")); //correct sort type? } function cmp($a, $b) { ...................................... missing code } Can anyone help??

    Read the article

  • Sort multidimension array in php by more than two fields

    - by Ankita Agrawal
    I want to sort array by two fields. I mean to say I have an array like :- Array ( [0] => Array ( [name] => abc [url] => http://127.0.0.1/abc/img1.png [count] => 69 [img] => accessoire-sets_1.jpg ) [1] => Array ( [name] => abc2 [url] => http://127.0.0.1/abc/img12.png [count] => 73 [img] => ) [2] => Array ( [name] => abc45 [url] => http://127.0.0.1/abc/img122.png [count] => 15 [img] => tomahawk-kopen_1.png ) [3] => Array ( [name] => zyz [url] => http://127.0.0.1/abc/img22.png [count] => 168 [img] => ) [4] => Array ( [name] => lmn [url] => http://127.0.0.1/abc/img1222.png [count] => 10 [img] => ) [5] => Array ( [name] => qqq [url] => http://127.0.0.1/abc/img1222.png [count] => 70 [img] => ) [6] => Array ( [name] => dsa [url] => http://127.0.0.1/abc/img1112.png [count] => 43 [img] => ) [7] => Array ( [name] => wer [url] => http://127.0.0.1/abc/img172.png [count] => 228 [img] => ) [8] => Array ( [name] => hhh [url] => http://127.0.0.1/abc/img126.png [count] => 36 [img] => ) [9] => Array ( [name] => rrrt [url] => http://127.0.0.1/abc/img12.png [count] => 51 [img] => ) [10] => Array ( [name] => yyy [url] => http://127.0.0.1/abc/img12.png [count] => 22 [img] => ) [11] => Array ( [name] => cxz [url] => http://127.0.0.1/abc/img12.png [count] => 41 [img] => ) [12] => Array ( [name] => tre [url] => http://127.0.0.1/abc/img12.png [count] => 32 [img] => ) [13] => Array ( [name] => fds [url] => http://127.0.0.1/abc/img12.png [count] => 10 [img] => ) ) array WITHOUT images (field "img" )should always be placed underneath array WITH images. After this there will be sorted on the amount of products (field count) in the array. Means I have to show sort array first on the basis of img then count. I am using usort( $childLinkCats, 'sortempty' );` function sortempty( $a, $b ) { return empty( $a['img'] ); } it will show array with image value above the one who contains null value. and to sort through count Im using usort($childLinkCats, "_sortByCount"); function _sortByCount($a, $b) { return strnatcmp($a['count'], $b['count']); } It will short by count But I am facing problem that only 1 working is working at a time, but I have to use both, please help me.

    Read the article

  • php - sort and delete duplicates?

    - by c41122ino
    I've got an array that looks like this: Array ( [0] => Array ( num => 09989, dis => 20 ) [1] => Array ( num => 09989, dis => 10 ) [2] => Array ( num => 56676, dis => 15 ) [3] => Array ( num => 44533, dis => 20 ) [4] => Array ( num => 44533, dis => 50 ) ) First, I'm trying to sort them by num, and can't seem to get the usort example from php.net working here. It simply doesn't appear to be sorting... I'm also trying to delete the array element if it's a duplicate and whose dis value is higher than the other one. So, based on the example above, I'm trying to create: Array ( [0] => Array ( num => 09989, dis => 10 ) [1] => Array ( num => 44533, dis => 20 ) [2] => Array ( num => 56676, dis => 15 ) ) This is the code from php.net: function cmp($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; }

    Read the article

  • PHP 5.2 Function needed for GENERIC sorting FOLLOWUP

    - by donbriggs
    OK, you guys gave me a great solution for sorting a recordset array last Friday. (http://stackoverflow.com/questions/2884325/php-5-2-function-needed-for-generic-sorting-of-a-recordset-array) But now when I implement it, I end up with an extra element in the recordset array. I won't wast space reposting the same info, as the link is above. But the bottom line is that when I sort an array of 5 records, the resulting array has 6 records. The last element in the array is not a record array, but rather just a element containing an integer value of 1. I presume that it is somehow getting the output value of the "strnatcasecmp" function, but I have no idea how it is happening. Here is the function that you fine folks provided last week: function getSortCommand($field, $sortfunc) { return create_function('$var1, $var2', 'return '.$sortfunc.'($var1["'.$field.'"], $var2["' .$field .'"]);'); } And here is the line I am calling to sort the array: $trek[] = usort($trek, getSortCommand('name', 'strnatcasecmp')); This produces the following output, with an extra element tacked on to the end. Array ( [0] = Array ( [name] = Kirk [shirt] = Gold [assign] = Bridge ) [1] => Array ( [name] => McCoy [shirt] => Blue [assign] => Sick Bay ) [2] => Array ( [name] => Scotty [shirt] => Red [assign] => Engineering ) [3] => Array ( [name] => Spock [shirt] => Blue [assign] => Bridge ) [4] => Array ( [name] => Uhura [shirt] => Red [assign] => Bridge ) [5] => 1 )

    Read the article

  • Drupal administration theme doesn't apply to Blocks pages (admin/build/block)

    - by hfidgen
    A site I'm creating for a customer in D6 has various images overlaying parts of the main content area. It looks very pretty and they have to be there for the general effect. The problem is, if you use this theme in the administration pages, the images get in the way of everything. My solution was to create a custom admin theme, based on the default one, which has these image areas disabled in the output template files - page.tpl.php The problem is that when you try and edit the blocks page, it uses the default theme and half the blocks admin settings are unclickable behind the images. I KNOW this is by design in Drupal, but it's annoying the hell out of me and is edging towards "bug" rather than "feature" in my mind. It also appears that there is no way of getting around it. You can edit /modules/blocks/block.admin.inc to force Drupal to show the blocks page in the chosen admin theme. BUT whichever changes you then make will not be transferred to the default theme, as Drupal treats each theme separately and each theme can have different block layouts. :x function block_admin_display($theme = NULL) { global $custom_theme; // If non-default theme configuration has been selected, set the custom theme. // $custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland'); // Display admin theme $custom_theme = variable_get('admin_theme', '0'); // Fetch and sort blocks $blocks = _block_rehash(); usort($blocks, '_block_compare'); return drupal_get_form('block_admin_display_form', $blocks, $theme); } Can anyone help? the only thing I can think of is to push the $content area well below the areas where the image appear and use blocks only for content display. Thanks!

    Read the article

  • Sort array by two specifics values in PHP

    - by Roger
    The folks have already showed me how to sort an array by a specific value using usort and a fallback function in PHP. What if this specifc value doesn't exist and we have to use two values? in the example bellow the values [4] and [5]... In other words, I want to do this: order all objects numericaly by the fith value of each object from the highest to the lowest, and addicionally, for those objects that have the fifht value is empty (in the examplem '-'), order them by the fourth value. Array( [0] => Array( [0] => links-patrocinados [1] => adwords [2] => 0,5 [3] => R$92,34 [4] => 823000 [5] => 49500 ) [1] => Array( [0] => adwords [1] => google adwords como funciona [2] => 0,38 [3] => R$0,20 [4] => 480 [5] => 480 ) [2] => Array( [0] => links-patrocinados [1] => adword [2] => 0,39 [3] => R$58,77 [4] => 49500 [5] => 2900 ) [3] => Array( [0] => agencia [1] => agencias viagens espanholas [2] => - [3] => R$0,20 [4] => 58 [5] => - ) [4] => Array( [0] => agencia [1] => era agencia imobiliaria [2] => - [3] => R$0,20 [4] => 73 [5] => - ) )

    Read the article

  • php sorting a seriously multidimensional array...

    - by BigDogsBarking
    I'm trying to sort a multidimensional object, and, after looking on php.net and around here, I get that I should write a function that I can then call via usort. I'm having some trouble with the syntax. I haven't ever written something this complicated before, and trying to figure it out feels like a mindbender... I'm working with the array posted at the end of this post. I want to filter out duplicate [n] values. But, and this is the tricky part for me, I want to keep the [n] value that has the smallest [d] value. So, if I have (and this example is simplified, the real array is at the end of this post): Array ( [7777] => Array ( [0] => Array ( [n] => '12345' [d] => 1 ) [1] => Array ( [n] => '67890' [d] => 4 ) ) [8888] => Array ( [2] => Array ( [n] => '12345' [d] => 10 ) [3] => Array ( [n] => '67890' [d] => 2 ) ) ) I want to filter out duplicate [n] values based on the [d] value, so that I wind up with this: Array ( [7777] => Array ( [0] => Array ( [n] => '12345' [d] => 1 ) ) [8888] => Array [3] => Array ( [n] => '67890' [d] => 2 ) ) ) I've tried writing different variations of the function cmp example posted on php.net, but I haven't been able to get any to work, and I think it's because I'm not altogether clear on how to traverse it using their example... I tried: function cmp($a, $b) { if($a['n'] == $b['n']) { if($a['d'] == $b['d']) { return 0; } } return ($a['n'] < $b['n']) ? -1 : 1; } But, that really did not work at all... Anyway, here's the real array I'm trying to work with... Help is greatly appreciated! Array ( [32112] => Array ( [0] => Array ( [n] => '02124' [d] => '0' ) [1] => Array ( [n] => '02124' [d] => '0.240101905123744' ) [2] => Array ( [n] => '11050' [d] => '0.441758632682761' ) [3] => Array ( [n] => '02186' [d] => '0.317514080260304' ) ) [43434] => Array ( [4] => Array ( [n] => '02124' [d] => '5.89936971664429e-05' ) [5] => Array ( [n] => '02124' [d] => '0.145859264792549' ) [6] => Array ( [n] => '11050' [d] => '0.327864593457739' ) [7] => Array ( [n] => '11050' [d] => '0.312135345168295' ) ) )

    Read the article

1