The two following usort functions throw fatal error Base lambda function for closure not found in our productive environment (PHP 5.4). This seems to be a known PHP bug that should be fixed by now (https://bugs.php.net/bug.php?id=52144), but it still occurs for us.

Anyway, we unfortunately don't have time to figure out what's wrong with our PHP configurations etc. We would like to rewrite these two functions without the use of anonymous functions, so that the error doesn't occur anymore.

1) Ordering of a multidimensional array ($array) by value of key "position":

// ARRAY BEFORE

Array
(
    [0] => Array
        (
            [ftid] => 3339685
            [position] => 2
            [auswahl] => 7179726
            [keine_antwort] => 
        )

    [1] => Array
        (
            [ftid] => 3339686
            [position] => 1
            [auswahl] => 7179727
            [keine_antwort] => 
        )
)
// FUNCTION THAT NEEDS TO BE REWRITTEN

usort($array, function($a, $b) {
    return $a['position'] - $b['position'];
});
// ARRAY AFTER

Array
(
    [0] => Array
        (
            [ftid] => 3339686
            [position] => 1
            [auswahl] => 7179727
            [keine_antwort] => 
        )

    [1] => Array
        (

            [ftid] => 3339685
            [position] => 2
            [auswahl] => 7179726
            [keine_antwort] => 
        )
)

2) Ordering of a multidimensional array ($array) according to the order of a second array ($position_order):

// $array before

Array
(
    [0] => Array
        (
            [ftid] => 3339685
            [position] => 1
            [auswahl] => 7179726
            [keine_antwort] => 
        )

    [1] => Array
        (
            [ftid] => 3339686
            [position] => 2
            [auswahl] => 7179727
            [keine_antwort] => 
        )

)

// $position_order (key value corresponds to key 'ftid' in $array

Array
(
    [3339686] => 1
    [3339685] => 2

)
// FUNCTION THAT NEEDS TO BE REWRITTEN

usort($array, function($a, $b) use($position_order) {
    return (isset($position_order[$a['ftid']]) ? ($position_order[$a['ftid']] - $position_order[$b['ftid']]) : 1);
});
// $array 

Array
(
    [0] => Array
        (
            [ftid] => 3339686
            [position] => 2
            [auswahl] => 7179727
            [keine_antwort] => 
        )

    [1] => Array
        (
            [ftid] => 3339685
            [position] => 1
            [auswahl] => 7179726
            [keine_antwort] => 
        )

)

Especially the latter causes some headache, as we don't know how to pass the "outside" array $position_order.

有帮助吗?

解决方案

Someone may come up with a better usort implementation, but at least for the first one, this is how I do it:

function array_column_sort(&$array, $column, $sort=SORT_ASC) {
    foreach($array as $key => $val) {
        $sort_array[$key] = $val[$column];
    }
    array_multisort($sort_array, $sort, $array);
}

Works for the second (may want better func names):

function array_column_sort_array(&$array, $column, $sort_array) {
    foreach($array as $val) {
        $result[$sort_array[$val[$column]]] = $val;
    }
    ksort($result);
    $array = array_values($result);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top