Search code examples
phparrayssortingmultidimensional-arrayksort

"Warning: ksort() expects at most 2 parameters, 3 given" while sorting a 2d array with array_walk($array, 'ksort')


How can apply ksort to each element of $counts array? I mean simply call this function on each row of a 2d array (not recursively).

bool array_walk(array &$array, callable $funcname [,mixed $userdata = NULL])

I've tried array_walk passing the flag SORT_NUMERIC as user data. This gives me a warning:

$counts = array();

$counts['group1'] = array(); // Keys are timestamps but as STRING
$counts['group2'] = array(); // Keys are timestamps but as STRING
// ...

// Array + operator does a key reordering here
$counts['group1'] += $dummyData;
$counts['group2'] += $dummyData;

// .. so sort array by keys
array_walk($counts, 'ksort', SORT_NUMERIC);

Warning: ksort() expects at most 2 parameters, 3 given.

What's the third parameter?


Solution

  • From http://php.net/manual/en/function.array-walk.php

    Typically, funcname takes on two parameters. The array parameter's value being the first, and the key/index second.

    If the optional userdata parameter is supplied, it will be passed as the third parameter to the callback funcname.