I'm new to PHP and hope people can help me get a better insight. I'm trying to resolve a problem I did in JS into PHP to better understand the syntax and am running to this error:
PHP fatal error: uncaught TypeError: Argument 1 passed to Serializer::serializeArray() must be of type array, null given, called in /leetcode/precomiled/serializer.php
This is my code. I hope someone can tell me what I'm doing wrong to better understand and avoid further PHP mistakes. Thank you to all who answer.
class Solution {
function twoSum($nums, $target) {
$collection = array();
foreach($nums as $key => $num) {
$subtracted = $target - $num;
if ($collection[$subtracted]) {
return array($collection[$subtracted], $key);
} else {
$collection[$num] = $key;
}
}
}
}
$key->$numlooks like a typo for$key => $num. I assume it's a copying error or you'd get other errors.