Dimension of a multi-dimensional mass, as weort()



  • I've got this mass.

    Array(
    array(
       'name' => 'title 1',
       'price' => 200
       'id' => '737362'
    )
    array(
       'name' => 'title 2',
       'price' => 100
       'id' => '737363'
    ))
    

    I needed a price grading. There's a code on the next subject:

    usort($array, function($a, $b){
    return ($a['price'] - $b['price']);});
    

    The code works great, no problem. But I can't understand how he works. Explain what's going on? Why are we reading?



  • Why are we reading?

    Very good question.

    We're counting because that's how it works. http://php.net/manual/ru/function.usort.php : The function we give as an argument should return a positive number if the first argument is greater than the second; negative - if less; and zero - if the arguments are equal. This, in fact, allows the function to understand how to rank the elements of the mass.

    Well, it's easier to get a result of this than to read the first Operand out of the other!

    The rows are harder to sort, and they have to use strcmp() which also returns 1-1 and 0 in the cases described.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2