eldan88 Posted August 5, 2014 Share Posted August 5, 2014 Hey Guys. I am trying to understand how the array_udiff works but I am having a hard time understanding why the call back function returns a interger? How does that return value preform the camparison? For example in the code below, what does the return 0 , or return 1 or -1 have anything to do with the comparison? Thank you in advance! function myfunction($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"blue","b"=>"black","e"=>"blue"); $result=array_udiff($a1,$a2,"myfunction"); print_r($result); Quote Link to comment Share on other sites More sharing options...
Barand Posted August 5, 2014 Share Posted August 5, 2014 Each pair of values is compared during the sort and passed to your function as $a and $b. Those return values indicate whether $a should sort above (-1) or below (+1) $b, or should be considered equal (0). (It doesn't have to be 1 and -1, any negative and positive numbers will have the same effect.) Quote Link to comment Share on other sites More sharing options...
eldan88 Posted August 7, 2014 Author Share Posted August 7, 2014 (edited) Hey Barand, Thanks for the explination but I still can't seem to figure it out. What do you mean exactly by "Sort Above" also why would it sore above if its a negative number??? Also how is it logically possible to check if $a is greater than $b??? Edited August 7, 2014 by eldan88 Quote Link to comment Share on other sites More sharing options...
Barand Posted August 7, 2014 Share Posted August 7, 2014 "Sort above" means "comes before" in the sorted output list. Also how is it logically possible to check if $a is greater than $b you could try using "if ($a > $b)" Quote Link to comment Share on other sites More sharing options...
Barand Posted August 7, 2014 Share Posted August 7, 2014 Perhaps an example will help. I have an array of products and prices and I want them in ascending order of price but those with zero price should be listed at the end. $data = array ( array ('prodcode' => 'A123', 'price' => 10), array ('prodcode' => 'E123', 'price' => 0), array ('prodcode' => 'D123', 'price' => 40), array ('prodcode' => 'C123', 'price' => 30), array ('prodcode' => 'E124', 'price' => 0), array ('prodcode' => 'B123', 'price' => 20) ); uasort ($data, 'mysort'); function mysort ($a, $b) { if ($a['price']==0) return 1; // $a should sort below $b elseif ($b['price']==0) return -1; // $a should sort above $b else return ($a['price'] - $b['price']); // $a sorts above $b if $a < $b } foreach ($data as $p) { echo "{$p['prodcode']} {$p['price']}<br>"; } /* OUTPUT: A123 10 B123 20 C123 30 D123 40 E124 0 E123 0 */ Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 7, 2014 Share Posted August 7, 2014 (edited) but those with zero price should be listed at the end. Yes, b/s every number is less than every capital and lower letters - http://www.asciitable.com/ function myfunction($a,$b) { if ($a===$b) { return 0; } return ($a > $b) ? 1 : -1; } $a = 'a'; $b = 'B'; var_dump(myfunction($a, $b)); // int(1) Capital "B" is less than lower 'a'. Hm.......NO....I am wrong <?php function myfunction($a,$b) { if ($a===$b) { return 0; } return ($a > $b) ? 1 : -1; } $a = 1; $b = 'a'; var_dump(myfunction($a, $b)); // int(1) But not in javascript: function MyFunction(a,b) { if(a === b) return 'equal values'; return (a < b) ? 'a1 is less than a2' : 'a1 is greater than a2'; } var a1 = 'a'; var a2 = 1; alert(MyFunction(a1,a2)) I made the first test in JS Sorry for that. Edited August 7, 2014 by jazzman1 Quote Link to comment Share on other sites More sharing options...
eldan88 Posted August 9, 2014 Author Share Posted August 9, 2014 Hey Barand. Thanks for providing the example. I understand it a little better, but I am stilll a little bit confused in some parts. 1) I don't understand how $a > $b if they are both strings and not numbers 2) Why would a negative number be sorted above $b isn't supposed to be below be since it a negative number? For example in the follwing example, how can $a be greater, less than or equal to $b?? .... That's what confuses me the most $a = array("Alex","jon"); $b = array("Mike", "jon"); echo "<pre>"; print_r(array_udiff($a,$b,function($a,$b){ if ($a < $b) { fb("I am coming out of the if (a < b)"); return -1; } elseif ($a > $b) { fb("I am coming out of the a > b"); return 1; } else { fb("I am coming out of the else"); return 0; }; })); Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted August 9, 2014 Solution Share Posted August 9, 2014 1. Strings have a sort order, just as numbers do. ABC is less than ACB So if you sort the list ACB ABC in your function if $a = 'ACB' and $b = 'ABC' $a is greater than $b so return +1 ie $a sorts below $b in the output list ABC ($b) ACB ($a) Quote Link to comment Share on other sites More sharing options...
eldan88 Posted August 13, 2014 Author Share Posted August 13, 2014 ahhh okay. I got it now. Thank you so much for making it more clear to me now Quote Link to comment 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.