scienceskillz Posted December 20, 2009 Share Posted December 20, 2009 Hi. I know sort($array) will sort a 1-D array. However, is there a built-in function like that to sort multidimensional array by one of the dimensions? for example $myarray = array(); $myarray['fruits'] = ['apple','orange','banana']; $myarray['expireDate'] = ['jan','march','february']; I want to sort fruits and expireDate based on expireDate.. so this pseudo function would return: somespecialsortfunction($myarray,'expireDate') $myarray['fruits'] = ['apple','banana','orange']; $myarray['expireDate'] = ['jan','february','march']; I realize the dates are just month strings but imagine I have actual dates inside the 'expireDate' array .... I hope I've made this clear enough... Thanks! SS Quote Link to comment https://forums.phpfreaks.com/topic/185799-sort-2-d-or-multi-dimensional-array/ Share on other sites More sharing options...
rajivgonsalves Posted December 20, 2009 Share Posted December 20, 2009 you should check out this function http://php.net/manual/en/function.array-multisort.php Quote Link to comment https://forums.phpfreaks.com/topic/185799-sort-2-d-or-multi-dimensional-array/#findComment-981066 Share on other sites More sharing options...
scienceskillz Posted December 20, 2009 Author Share Posted December 20, 2009 Perfect! Thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/185799-sort-2-d-or-multi-dimensional-array/#findComment-981068 Share on other sites More sharing options...
scienceskillz Posted December 20, 2009 Author Share Posted December 20, 2009 What about a multi-dimensional unique function? Does that exist? multi_array_unique($arr1,$arr2) Quote Link to comment https://forums.phpfreaks.com/topic/185799-sort-2-d-or-multi-dimensional-array/#findComment-981179 Share on other sites More sharing options...
salathe Posted December 20, 2009 Share Posted December 20, 2009 How would such a function work? There isn't one that I'm aware of if what I think you're wanting is correct. However, as with most things like this, it's generally only a matter of a few lines of code to implement such a function yourself. Quote Link to comment https://forums.phpfreaks.com/topic/185799-sort-2-d-or-multi-dimensional-array/#findComment-981181 Share on other sites More sharing options...
scienceskillz Posted December 21, 2009 Author Share Posted December 21, 2009 Hi, Well sure I guess I could implement something like this myself but I just figured maybe something was already out there. What I want is the following: given an array $arr = array('2','2','hi'); array_unique($arr)... would make $arr = array('2','hi'); but.. what I require is multi-dimension application. So... $arr['somekey'] = array('2','2','hi'); $arr['someotherkey'] = array('blah','blah2','blah3'); array_unique($arr['somekey'],$arr['someotherkey'])... Would make... $arr['somekey'] = array('2','hi'); and $arr['someotherkey'] = array('blah','blah3'); Quote Link to comment https://forums.phpfreaks.com/topic/185799-sort-2-d-or-multi-dimensional-array/#findComment-981221 Share on other sites More sharing options...
Zane Posted December 21, 2009 Share Posted December 21, 2009 array_map() $parameters[] = $array1; $parameters[] = $array2; $parameters[] = $array3; $parameters[] = $array4; $parameters[] = $array5; $parameters[] = $array6; $newArray = array_map('array_unique', $parameters); Quote Link to comment https://forums.phpfreaks.com/topic/185799-sort-2-d-or-multi-dimensional-array/#findComment-981228 Share on other sites More sharing options...
scienceskillz Posted December 21, 2009 Author Share Posted December 21, 2009 Hmm.. that seems like it just makes each array unique but not necessarily re-defines each array based on the uniqueness of one... for example.. $parameters[] = array('1','2','1'); $parameters[] = array('hi','yo','bla'); $newArray = array_map('array_unique', $parameters); var_dump($newArray); outputs: array(2) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "2" } [1]=> array(3) { [0]=> string(2) "hi" [1]=> string(2) "yo" [2]=> string(3) "bla" } } but I want it to output: array(2) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "2" } [1]=> array(3) { [0]=> string(2) "hi" [1]=> string(2) "yo" } } because 1 is duplicated and it needs to redefine the second array accordingly to not include 'bla' Quote Link to comment https://forums.phpfreaks.com/topic/185799-sort-2-d-or-multi-dimensional-array/#findComment-981234 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.