wyclef Posted March 14, 2023 Share Posted March 14, 2023 (edited) Hello, I’m getting a fatal error when upgrading from PHP 7.4 to PHP 8: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in... on the following count line. function orderByDate($items) { //$tempDate = strtotime($item); $referenceArray = array(); for ($i = 0; $i < count($items); $i++) { $referenceArray[] = strtotime($items[$i]['pubDate']); } array_multisort($referenceArray, SORT_DESC, $items); return $items; } Anyone know how to resolve this? Edited March 14, 2023 by wyclef Quote Link to comment https://forums.phpfreaks.com/topic/316009-php-8-fatal-error/ Share on other sites More sharing options...
Barand Posted March 14, 2023 Share Posted March 14, 2023 According to the error message, $items is NULL. You need to find where your function is being called and investigate why the item being passed to the function has no value assigned, and put some checking code in to prevent it reoccurring. Quote Link to comment https://forums.phpfreaks.com/topic/316009-php-8-fatal-error/#findComment-1606532 Share on other sites More sharing options...
wyclef Posted March 14, 2023 Author Share Posted March 14, 2023 I think this fixes it? testing now... how does this look? function orderByDate($items) { $referenceArray = array(); for ($i = 0; $i < (is_countable($items) ? count($items) : 0); $i++) { $referenceArray[] = strtotime($items[$i]['pubDate']); } array_multisort($referenceArray, SORT_DESC, $items); return $items; } Quote Link to comment https://forums.phpfreaks.com/topic/316009-php-8-fatal-error/#findComment-1606534 Share on other sites More sharing options...
Barand Posted March 14, 2023 Share Posted March 14, 2023 28 minutes ago, wyclef said: ... how does this look? It looks like you gave it a painkiller instead of curing the source of the pain. You should be checking why you are calling a function (that expects an array) with a variable that isn't an array. Quote Link to comment https://forums.phpfreaks.com/topic/316009-php-8-fatal-error/#findComment-1606535 Share on other sites More sharing options...
ginerjm Posted March 14, 2023 Share Posted March 14, 2023 As Barand has said - you really should have gone into your code to find the places that this function is being called and ensure that you are sending an array to it. Obviously your approach has a 'bug' in it to be sending the invalid argument to this function. OTOH - if you don't want to do this work - you could have simply added a test at the beginning of the function for is_array() and if it fails simply return a null or whatever you are expecting from this function. Your convoluted expression makes for difficult reading when someone else comes along and tries to figure out your code. As in: function orderByDate($items) { if (!is_array($items)) return null; // or something ... ... ... Quote Link to comment https://forums.phpfreaks.com/topic/316009-php-8-fatal-error/#findComment-1606536 Share on other sites More sharing options...
gizmola Posted March 15, 2023 Share Posted March 15, 2023 Let's assume it is ok for null to be passed sometimes, and you aren't able to hunt that down and fix it. A better fix would be: <?php function orderByDate(?array $items): array { $items = $items ?? []; $referenceArray = array(); foreach ($items as $item) { $referenceArray[] = strtotime($item['pubDate']); } array_multisort($referenceArray, SORT_DESC, $items); return $items; } This eliminates the need for a for loop and counter, adds the php7/8 typehints, and uses the null coalescing operator to handle a parameter when it's null. This will allow either a valid array or null, and the end result is that you will get an empty array returned if null was passed, which I assume was the way it used to function. A unit test would be better, but here's a little test script of the function demonstrating that it works as expected: $t = [ ['pubDate' => '10 September 2000', 'name' => 'apple'], ['pubDate' => 'now', 'name' => 'banana'], ['pubDate' => '+1 week', 'name' => 'coconut'] ]; var_dump($t); $t = orderByDate($t); var_dump($t); $t = orderByDate(null); var_dump($t); Output: array(3) { [0]=> array(2) { ["pubDate"]=> string(17) "10 September 2000" ["name"]=> string(5) "apple" } [1]=> array(2) { ["pubDate"]=> string(3) "now" ["name"]=> string(6) "banana" } [2]=> array(2) { ["pubDate"]=> string(7) "+1 week" ["name"]=> string(7) "coconut" } } array(3) { [0]=> array(2) { ["pubDate"]=> string(7) "+1 week" ["name"]=> string(7) "coconut" } [1]=> array(2) { ["pubDate"]=> string(3) "now" ["name"]=> string(6) "banana" } [2]=> array(2) { ["pubDate"]=> string(17) "10 September 2000" ["name"]=> string(5) "apple" } } array(0) { } array_multisort is a strange, non-intuitive function, but it does perform some magic in this case. Quote Link to comment https://forums.phpfreaks.com/topic/316009-php-8-fatal-error/#findComment-1606544 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.