Laravel PHP obtain a minimum value



  • There is a string of this type

    array(1) { [0]=> string(108) "Айти|501.64|Small|412.77|Лик|515.00" }
    

    How does this field get a minimum value, that is 412.77?



  • Is that a good option?

    $str = "Айти|501.64|Small|412.77|Лик|515.00";
    echo min(explode("|", $str));
    

    Well, for your example, one line.

    echo min(explode("|", $array[0]));
    

    UPDATE

    It turns out that the author needs a couple, the name of Vergotsifra.

    Then the solution is:

    preg_match_all('/(.*?)?\|([\d\.]+)?/', $array[0], $m, PREG_SET_ORDER);
    $t = Array();
    foreach($m as $pair) {
        if (Count($pair)<=2) continue;
        $t[$pair[1]] = $pair[2];
    }
    asort($t);
    echo "Min value: " . current($t) . "<br />";
    echo "Min name: " . key($t) . "<br />";
    
    • With help regex We're pulling couples out.
    • We're moving from the association.
    • We sort the volume by weight
    • We take first meaning and first key.

    This decision has a flaw: it may not work properly if there are repeated names.


Log in to reply
 


Suggested Topics

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