15ongm 15ongm - 26 days ago 11
PHP Question

Why is array_combine not returning an array?

When I try to print the array returned from this function, I get a blank screen.

My array

$terms
and
$definitions
are both the same length and they exist before and after I call
make_associative_array()
.

function make_associative_array() {
return array_combine($terms, $definitions);
}

$c = make_associative_array();
print_r($c);


$terms:

Array (
[0] => Nock (verb) [1] => End [2] => Serving [3] => Nock (noun)
)


$definitions:

Array (
[0] => To place an arrow against the string prior to shooting. [1] => A group of arrows shot during a tournament. Usually 6. [2] => Thread wound around a bow string to protect the string. [3] => A notch at the rear of an arrow. The bow string is placed in the nock.
)


I am using PHP 5.6.27

Answer

In your case - array_combine returns NULL because both $terms & $definitions are null inside the scope of make_associative_array.

You can either make them global:

function make_associative_array() {
    global $terms, $definitions;
    return array_combine($terms, $definitions);
}

Or pass them to the function:

function make_associative_array($terms, $definitions) {
    return array_combine($terms, $definitions);
}
$c = make_associative_array($terms, $definitions);

Anyway - I really advise you to turn on errors:
http://sandbox.onlinephpfunctions.com/code/40cfd2d197aebd4d935c793c1ea662cab50ce8b1

Comments