Deprecated: Function create_function() is deprecated
$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!
Quote:
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.
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:
NB. de bouwer van deze module is niet-bereikbaar en ik 'spreek' te weinig PHP om dit op te lossen. Vandaar de vraag.
Quote:
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;
}
// 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;
}
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?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;
}
?>
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?
Die had ik gelezen, maar lukt me nog niet deze te vertalen naar een oplossing van de foutmelding.Dit al gelezen?
Quote:
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.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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;
}
?>
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:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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;
}
?>
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
Quote:
en dat gebeurt op deze regel:Notice: Undefined property: stdClass::$field
Quote:
? $a->field <=> $b->$field // if "numeric" sort type
Toevoeging op 26/05/2019 03:09:35:
Rob Doemaarwat op 24/05/2019 19:52:30:
Nou, vooruit dan, rondje van de zaak:
Garantie tot de deur. Ik kreeg in ieder geval geen parse errors (ship it!), maar ik had geen data om mee te testen.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?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;
}
?>
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!