4

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;
            }
        }
    }
}
8
  • 2
    Not sure where Serializer::serializeArray() is happening (not in this code), but it's probably because your function doesn't return anything. Commented Jan 18, 2019 at 21:54
  • A PHP function implicitly returns null if you don't tell it to return something, and there are some possible paths through your function where nothing will be returned. Commented Jan 18, 2019 at 21:55
  • 1
    @Steven, what is the function actually supposed to do? I think I can sort of tell from context, but not sure Commented Jan 18, 2019 at 22:06
  • 1
    $key->$num looks like a typo for $key => $num. I assume it's a copying error or you'd get other errors. Commented Jan 18, 2019 at 22:07
  • 1
    What is it supposed to return if it can't find any values that fit the criteria? Commented Jan 18, 2019 at 22:27

1 Answer 1

5

Your function doesn't work correctly when the pair of numbers includes the first element of the array. You use:

if ($collection[$subtracted])

to test if $subtracted is a key of the array. But if the value of $collection[$subtracted] is 0, that will also fail the if test. Change that to:

if (isset($collection[$subtracted]))

You should also add:

return array();

after the for loop, so that it returns an empty array by default if no matching elements are found.

function twoSum($nums, $target) {
    $collection = array();

    foreach($nums as $key => $num) {
        $subtracted = $target - $num;

        if (isset($collection[$subtracted])) {
            return array($collection[$subtracted], $key);  
        } else {
            $collection[$num] = $key;
        }
    }

    return array();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Makes perfect sense. It was producing a 0 index which caused that error. Thank you. I'll need to research what isset is in order to better use that function for the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.