Ramcult Posted December 7, 2021 Share Posted December 7, 2021 <?php $raw[] = array("hidden" => true, "label" => "Lorem"); $raw[] = array("hidden" => true, "label" => "Ipsum"); $raw[] = array("hidden" => false, "label" => "Dolor"); $raw[] = array("hidden" => false, "label" => "Sit"); $raw[] = array("hidden" => true, "label" => "Amet"); $raw[] = array("hidden" => true, "label" => "Consectetur"); $raw[] = array("hidden" => false, "label" => "Adipiscing"); $raw[] = array("hidden" => false, "label" => "Elit"); $raw[] = array("hidden" => true, "label" => "Sed"); print_r($raw); ?> Here is data array.. Help please. :) Quote Link to comment https://forums.phpfreaks.com/topic/314283-how-to-sort-hidden-a-z-in-top-and-show-z-a-in-bottom/ Share on other sites More sharing options...
requinix Posted December 7, 2021 Share Posted December 7, 2021 Use usort. Quote Link to comment https://forums.phpfreaks.com/topic/314283-how-to-sort-hidden-a-z-in-top-and-show-z-a-in-bottom/#findComment-1592521 Share on other sites More sharing options...
Ramcult Posted December 7, 2021 Author Share Posted December 7, 2021 how to use it ? can u explain with code ? Quote Link to comment https://forums.phpfreaks.com/topic/314283-how-to-sort-hidden-a-z-in-top-and-show-z-a-in-bottom/#findComment-1592522 Share on other sites More sharing options...
Phi11W Posted December 7, 2021 Share Posted December 7, 2021 2 hours ago, Ramcult said: how to use it ? can u explain with code ? Seriously? Did you even look at the Manual Page that requinix pointed you to? Example #2, down in the comments, gives a worked example of how to define a function that compares two elements of an array, and then invoke usort against an input array and that named function. How to use it, explained with code. As requested. Regards, Phill W. Quote Link to comment https://forums.phpfreaks.com/topic/314283-how-to-sort-hidden-a-z-in-top-and-show-z-a-in-bottom/#findComment-1592523 Share on other sites More sharing options...
Barand Posted December 7, 2021 Share Posted December 7, 2021 If you want to sort by "hidden" you do not need usort(). A simple sort() or rsort() will suffice. By default, arrays are sorted on the value of the first element. $raw[] = array("hidden" => true, "label" => "Lorem"); $raw[] = array("hidden" => true, "label" => "Ipsum"); $raw[] = array("hidden" => false, "label" => "Dolor"); $raw[] = array("hidden" => false, "label" => "Sit"); $raw[] = array("hidden" => true, "label" => "Amet"); $raw[] = array("hidden" => true, "label" => "Consectetur"); $raw[] = array("hidden" => false, "label" => "Adipiscing"); $raw[] = array("hidden" => false, "label" => "Elit"); $raw[] = array("hidden" => true, "label" => "Sed"); sort($raw); showResults($raw); rsort($raw); showResults($raw); function showResults($array) { echo '<pre>'; printf("| %6s | %-15s |<br><br>", 'Hidden', 'Label'); foreach ($array as $arr) { printf("| %6s | %-15s |<br>", $arr['hidden'], $arr['label']); } echo "<pre><br>"; } Results sort() rsort() | Hidden | Label | | Hidden | Label | | | Adipiscing | | 1 | Sed | | | Dolor | | 1 | Lorem | | | Elit | | 1 | Ipsum | | | Sit | | 1 | Consectetur | | 1 | Amet | | 1 | Amet | | 1 | Consectetur | | | Sit | | 1 | Ipsum | | | Elit | | 1 | Lorem | | | Dolor | | 1 | Sed | | | Adipiscing | 1 Quote Link to comment https://forums.phpfreaks.com/topic/314283-how-to-sort-hidden-a-z-in-top-and-show-z-a-in-bottom/#findComment-1592524 Share on other sites More sharing options...
requinix Posted December 7, 2021 Share Posted December 7, 2021 7 hours ago, Barand said: If you want to sort by "hidden" you do not need usort(). A simple sort() or rsort() will suffice. If the arrays are crafted in a very specific way, ie. with the fields set in the order that they should be sorted on. A custom sort is a little (still a one-liner) more complicated but won't suddenly break if the arrays are built differently. Quote Link to comment https://forums.phpfreaks.com/topic/314283-how-to-sort-hidden-a-z-in-top-and-show-z-a-in-bottom/#findComment-1592532 Share on other sites More sharing options...
Ramcult Posted December 8, 2021 Author Share Posted December 8, 2021 13 hours ago, Barand said: sort($raw); showResults($raw); rsort($raw); showResults($raw); function showResults($array) { echo '<pre>'; printf("| %6s | %-15s |<br><br>", 'Hidden', 'Label'); foreach ($array as $arr) { printf("| %6s | %-15s |<br>", $arr['hidden'], $arr['label']); } echo "<pre><br>"; } Sorry, im junior in PHP. would you like to explain with code for the condition, this array data have a Hidden:true(1,null) and Label: String. i need to regroup the data Hidden and Label first. ex logic result and the output: 1. Group Hidden and Show 2. $raw have (a,b) 3. a = Hidden -> A-Z into top b = Show -> Z-A into top Sort hidden into top with A-Z (ASC) Sort Show into bottom with Z-A (DESC) 1 = Hidden | ASC = Show | DESC 1 Amet 1 Consectetur 1 Ipsum 1 Lorem 1 Sed Adipiscing Dolor Elit Sit I don't know how to explain it with code. Can you help me to explain this with code? Quote Link to comment https://forums.phpfreaks.com/topic/314283-how-to-sort-hidden-a-z-in-top-and-show-z-a-in-bottom/#findComment-1592535 Share on other sites More sharing options...
Barand Posted December 8, 2021 Share Posted December 8, 2021 7 hours ago, Ramcult said: Sort hidden into top with A-Z (ASC) Sort Show into bottom with Z-A (DESC) That would give this, (not what your example above shows) | 1 | Amet \ | 1 | Consectetur | | 1 | Ipsum | HIDDEN, A-Z | 1 | Lorem | | 1 | Sed / | | Sit \ | | Elit | SHOW, Z-A | | Dolor | | | Adipiscing / Is that what you want? If it is then this is the function... usort($raw, function ($a, $b) { $x = $b['hidden'] <=> $a['hidden']; // sort hidden to top, show to bottom (ie hidden DESC) if ($x == 0) { // do elements have same hidden value? return $a['hidden'] ? $a['label'] <=> $b['label'] : $b['label'] <=> $a['label']; // sort label ASC if hidden, label DESC if show } else return $x; }); If it isn't, adapt the function to your requirements. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314283-how-to-sort-hidden-a-z-in-top-and-show-z-a-in-bottom/#findComment-1592545 Share on other sites More sharing options...
Ramcult Posted December 9, 2021 Author Share Posted December 9, 2021 how to separate column in data array? For example, the step to separate the data in the hidden column becomes Hidden = True Hidden = False? ex: Hidden = Lorem, Dolor, Sed. Show = Ipsum, Sit, Amet. and then, initialize all $raw data to (a and b) ex: A = Hidden B = Show after that, retrieve data from (a and b) then (a and b) are sorted based on the data (a and b) ex: A = Dolor, Lorem, Sed B = Sit, Ipsum, Amet Quote Link to comment https://forums.phpfreaks.com/topic/314283-how-to-sort-hidden-a-z-in-top-and-show-z-a-in-bottom/#findComment-1592554 Share on other sites More sharing options...
Barand Posted December 9, 2021 Share Posted December 9, 2021 If you haven't written a custom sort function before, I suggest you start with something simple until you understand how they work. EG $array = [2, 1, 4, 3, 6, 5, 8, 7]; Then write functions to sort the above array ascending order descending order even numbers first followed by the odd numbers NOTE: A custom sort function has two arguments, $a and $b. usort() will place each pair of array items into $a and $b and call your function Your function should determine if $a should sort above or below $b by returning -1 (or a value < 0) if $a should be above $b +1 (or a value > 0) if $a should be below $b 0 if they are considered equal 1 Quote Link to comment https://forums.phpfreaks.com/topic/314283-how-to-sort-hidden-a-z-in-top-and-show-z-a-in-bottom/#findComment-1592563 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.