15ongm 15ongm - 1 year ago 72
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

are both the same length and they exist before and after I call

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

$c = make_associative_array();


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


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 Source

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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download