Heb mijn PHP versie naar 7.2 moeten upgraden om het cms te late werken en nu krijg ik in een van de programma's een fout melding dat create_function niet meer wordt ondersteund. De regel die de mist in gaat is:
$compare = create_function('$a, $b', $code);

Iemand die wil aangeven hoe deze regel herschreven kan worden zodat 7.2 het ook akkoord vindt?

Alvast dank!
$compare = function($a,$b){
  return eval($code);
}

Maar beter is het om de $code gewoon uit te schrijven (als dat kan; als het het niets "magisch" is wat weer uit een database, of andere hoek van je programma komt). Dus stel dat:

$code = '$a <=> $b';
//dan wordt:
$compare = function($a,$b){
  return $a <=> $b;
}
Dank je Rob. Ik zie o.b.v. jouw reactie dat ik de beter direct de hele functie had kunnen plaatsen omdat het tot een andere error leidt en $code in het vervolg van de functie nog nodig is.
NB. de bouwer van deze module is niet-bereikbaar en ik 'spreek' te weinig PHP om dit op te lossen. Vandaar de vraag.

public static function ArraySort($array, $arguments = array(), $keys = true) {
// source: http://nl2.php.net/manual/en/function.uasort.php#42723
// comparing function code
$code = "\$result=0; ";

// foreach sorting argument (array key)
foreach ($arguments as $argument)
{
if (!empty($argument))
{
// order field
$field = substr($argument, 2, strlen($argument));

// sort type ("s" -> string, "n" -> numeric)
$type = $argument[0];

// sort order ("+" -> "ASC", "-" -> "DESC")
$order = $argument[1];

// add "if" statement, which checks if this argument should be used
$code .= "if (!Is_Numeric(\$result) || \$result == 0) ";

// if "numeric" sort type
if (strtolower($type) == "n")
{
$code .= $order == "-" ? "\$result = (\$a->{$field} > \$b->{$field} ? -1 : (\$a->{$field} < \$b->{$field} ? 1 : 0));" : "\$result = (\$a->{$field} > \$b->{$field} ? 1 : (\$a->{$field} < \$b->{$field} ? -1 : 0)); ";
}
else
{
// if "string" sort type
$code .= $order == "-" ? "\$result = strcoll(\$a->{$field}, \$b->{$field}) * -1;" : "\$result = strcoll(\$a->{$field}, \$b->{$field}); ";
}
}
}
// return result
$code .= "return \$result;";

// create comparing function
$compare = create_function('$a, $b', $code);

// sort array and preserve keys
uasort($array, $compare);

// return array
return $array;
}
Nou, vooruit dan, rondje van de zaak:
<?php

public static function ArraySort($array, $arguments = array(), $keys = true) {
  uasort($array,function($a,$b) use ($arguments){
    $result = 0;

    foreach ($arguments as $argument) if (!empty($argument)){

      if (!Is_Numeric($result) || $result == 0){
        // order field
        $field = substr($argument, 2, strlen($argument));
        // sort type ("s" -> string, "n" -> numeric)
        $type = $argument[0];
        // sort order ("+" -> "ASC", "-" -> "DESC")
        $order = $argument[1];

        if (strtolower($type) == "n"){ // if "numeric" sort type
          $result = ($order == "-")
            ? ($a->$field > $b->$field ? -1 : ($a->$field < $b->$field ? 1 : 0))
            : ($a->$field > $b->$field ? 1 : ($a->$field < $b->$field ? -1 : 0));
        }
        else // if "string" sort type
        {
          $result = ($order == "-")
            ? strcoll($a->$field, $b->$field) * -1
            : strcoll($a->$field, $b->$field);
        }
      }
    }

    return $result;
  });

  // return array
  return $array;
}

?>

Garantie tot de deur. Ik kreeg in ieder geval geen parse errors (ship it!), maar ik had geen data om mee te testen.
- Ariën - op 24/05/2019 17:30:46


Dit al gelezen?
If you are using PHP 5.3.0 or newer a native [url=https://www.php.net/manual/en/functions.anonymous.php[/url] anonymous function should be used instead.

Die had ik gelezen, maar lukt me nog niet deze te vertalen naar een oplossing van de foutmelding.

Dat kan korter:
<?php

public static function ArraySort($array, $arguments = array(), $keys = true) {
  uasort($array,function($a,$b) use ($arguments){
    $result = 0;

    foreach ($arguments as $argument) if (!empty($argument)){
      $field = substr($argument, 2, strlen($argument));

      if($result = (strtolower($argument[0]) == "n")
        ? $a->field <=> $b->$field // if "numeric" sort type
        : strcoll($a->$field, $b->$field) // if "string" sort 
      ){
        if ($argument[1] == "-") // sort order ("+" -> "ASC", "-" -> "DESC")
          $result *= -1;

        break;
      }
    }

    return $result;
  });

  // return array
  return $array;
}

?>
Rob Doemaarwat op 25/05/2019 13:06:08

Dat kan korter:
<?php

public static function ArraySort($array, $arguments = array(), $keys = true) {
  uasort($array,function($a,$b) use ($arguments){
    $result = 0;

    foreach ($arguments as $argument) if (!empty($argument)){
      $field = substr($argument, 2, strlen($argument));

      if($result = (strtolower($argument[0]) == "n")
        ? $a->field <=> $b->$field // if "numeric" sort type
        : strcoll($a->$field, $b->$field) // if "string" sort 
      ){
        if ($argument[1] == "-") // sort order ("+" -> "ASC", "-" -> "DESC")
          $result *= -1;

        break;
      }
    }

    return $result;
  });

  // return array
  return $array;
}

?>


DDank je Rob. Deze leidt alleen tot een foutmelding
Notice: Undefined property: stdClass::$field
en dat gebeurt op deze regel:
? $a->field <=> $b->$field // if "numeric" sort type


[size=xsmall]Toevoeging op 26/05/2019 03:09:35:[/size]

Rob Doemaarwat op 24/05/2019 19:52:30

Nou, vooruit dan, rondje van de zaak:
<?php

public static function ArraySort($array, $arguments = array(), $keys = true) {
  uasort($array,function($a,$b) use ($arguments){
    $result = 0;

    foreach ($arguments as $argument) if (!empty($argument)){

      if (!Is_Numeric($result) || $result == 0){
        // order field
        $field = substr($argument, 2, strlen($argument));
        // sort type ("s" -> string, "n" -> numeric)
        $type = $argument[0];
        // sort order ("+" -> "ASC", "-" -> "DESC")
        $order = $argument[1];

        if (strtolower($type) == "n"){ // if "numeric" sort type
          $result = ($order == "-")
            ? ($a->$field > $b->$field ? -1 : ($a->$field < $b->$field ? 1 : 0))
            : ($a->$field > $b->$field ? 1 : ($a->$field < $b->$field ? -1 : 0));
        }
        else // if "string" sort type
        {
          $result = ($order == "-")
            ? strcoll($a->$field, $b->$field) * -1
            : strcoll($a->$field, $b->$field);
        }
      }
    }

    return $result;
  });

  // return array
  return $array;
}

?>

Garantie tot de deur. Ik kreeg in ieder geval geen parse errors (ship it!), maar ik had geen data om mee te testen.

Een rondje van de zaak doet het altijd goed ;) Dank je! Deze doet het!

Reageren