Areg
Areg

Reputation: 1485

Unknown "usort() expects parameter 1 to be array, null given" error

today im trying to sort array by passing a variable into its function. I can print and interact with the array just fine, but for some reason i cant pass it to a function.

I've tried printing the array to see if it actually has any data and it turns out it has all the data i need.

Here's where i initialize $filesinfo variable

<?php
  $path = 'uploads/'.$file;
  $LastModified = filemtime($path);
  $filesize = filesize($path);
  $filextension = explode(".",$file);
  $filetp = end($filextension);
  $filesinfo[] = array($path, $LastModified, $filesize, $filetp);
  $size = filesize($path);
  $units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  $power = $size > 0 ? floor(log($size, 1024)) : 0;
  echo number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power]; ?></td>
  <td><?php echo date("F d Y H:i:s",filemtime("uploads/".$file));?></td>
  <td><?php  $ext = explode(".",$file); echo end($ext);?></td>
  <td class="actions"><?php echo '<a class="btn btn-success" href="rename.php?name='.$file.'">Rename</a><form action="main.php" method="POST"> <button class="btn btn-danger" type="submit" name="del" value="'.$file.'">Delete</button></form>';?></td>
</tr>
<?php
  }
}
?>
<?php endforeach; ?>

And here's the place where i am trying to pass the $filesinfo to a function and it says that the parameter is null.

<?php
print_r($filesinfo);
if ($_GET['sort']==="date" && $_GET['type']==="desc") {
  SortByDate($filesinfo);
}elseif($_GET['sort']=="date" && $_GET['type']=="asc"){
  SortByDateAsc($filesinfo);
}elseif($_GET['sort']=="name" && $_GET['type']=="desc"){
  SortByNameDsc($filesinfo);
}elseif($_GET['sort']=="name" && $_GET['type']=="asc"){
  SortByNameAsc($filesinfo);
}elseif($_GET['sort']=="size" && $_GET['type']=="desc"){
  SortBySizeDsc($filesinfo);
}elseif($_GET['sort']=="size" && $_GET['type']=="asc"){
  SortBySizeAsc($filesinfo);
}elseif($_GET['sort']=="type" && $_GET['type']=="asc"){
  SortByTypeAsc($filesinfo);
}elseif($_GET['sort']=="type" && $_GET['type']=="desc"){
  SortByTypeDsc($filesinfo);
}

}

And here's one of my functions.


function SortByDate(&$Files) {
usort($Files, function($a, $b) {
return $a['1'] <=> $b['1'];
});
}

All of these functions are almost identical they just change the way the content is sorted and returns values. But i still cant put $filesinfo as a parameter even when it is an array.

UPDATE : Sorry posted wrong function, here's the output from print

Array ( [0] => Array ( [0] => uploads/bootstrap-45c98b856dc045.zip [1] => 1553512534 [2] => 2705432 [3] => zip ) [1] => Array ( [0] => uploads/pointillist5c94d2e7eec5d.bmp [1] => 1553257191 [2] => 1080054 [3] => bmp ) [2] => Array ( [0] => uploads/sfdes.jpg [1] => 1553265149 [2] => 1246909 [3] => jpg ) [3] => Array ( [0] => uploads/ssser.sql [1] => 1553239646 [2] => 13098 [3] => sql ) [4] => Array ( [0] => uploads/vvv.jpg [1] => 1553263350 [2] => 879394 [3] => jpg ) [5] => Array ( [0] => uploads/wadaw.jpg [1] => 1553264937 [2] => 1246909 [3] => jpg ) [6] => Array ( [0] => uploads/wadawv.mp3 [1] => 1553263463 [2] => 4113874 [3] => mp3 ) ) 

Upvotes: 1

Views: 2680

Answers (2)

Areg
Areg

Reputation: 1485

This is so stupid, i switched to xampp and it worked fine. I don't know what was the problem, might have been some request problem.

Upvotes: 1

Aniket Sahrawat
Aniket Sahrawat

Reputation: 12937

Description:

usort — Sort an array by values using a user-defined comparison function

Declaration:

usort ( array &$array , callable $value_compare_func ) : bool

The correct way of using usort is:

// comparator which will be passed to usort
function comparator($a, $b) {
    return ($a < $b) ? -1 : $a == $b ? 0 : 1;
}

$arr = array(1, 3, 5, 1);

usort($arr, "comparator"); // passing the comparator

Upvotes: 3

Related Questions