Jump to content

Need help using the array_udiff()


eldan88
Go to solution Solved by Barand,

Recommended Posts

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);
Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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 by eldan88
Link to comment
Share on other sites

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
*/
Link to comment
Share on other sites

 

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 by jazzman1
Link to comment
Share on other sites

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;

    };
}));

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.