0

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

Dekel
  • 60,707
  • 10
  • 101
  • 129
14wml
  • 4,048
  • 11
  • 49
  • 97

2 Answers2

1

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

Dekel
  • 60,707
  • 10
  • 101
  • 129
1

You have to pass parameter to function

 <?php
    function make_associative_array($terms,$definitions) {

        return array_combine($terms, $definitions);
    }

    $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.' 
    );

    $c = make_associative_array($terms,$definitions);
    echo "<pre>";
    print_r($c);

output will be

Array
(
    [Nock (verb)] => To place an arrow against the string prior to shooting.
    [End] => A group of arrows shot during a tournament. Usually 6.
    [Serving] => Thread wound around a bow string to protect the string.
    [Nock (noun) ] => A notch at the rear of an arrow. The bow string is placed in the nock.
)
Vision Coderz
  • 8,257
  • 5
  • 41
  • 57
  • There is no need to repeat an answer that already exists (you can just vote the existing answer, you know...) – Dekel Nov 06 '16 at 02:53
  • @Dekel. i have not repeated answer .i given answer by executing hist code in localhost.if we exicute in localhost and while posting it will take time it will take time when we post – Vision Coderz Nov 06 '16 at 02:54
  • Your answer is exactly the one I already wrote. can you explain the differences? – Dekel Nov 06 '16 at 02:55
  • @Dekel.sorry .i dont want argue .sorry. i have updated his array string and exicuted and given output. i have not asked op to accept or vote my answer its his interest. – Vision Coderz Nov 06 '16 at 02:57
  • That's not the issue here... and it's not about arguing. If an answer already exists there is not reason to repeat it. If you think your answer is different - just say how exactly it is. – Dekel Nov 06 '16 at 02:58
  • @Dekel.what is your issue brother. i dont know why you are arguing.Its my wish to post my answer i have not copied your answer or written same like your answer. There is no rule in stack that ou cant post any answer if answer is same . i told you i have posted answer by typing in localhost and when we type answer page will open but you are good in php so you posted before me. – Vision Coderz Nov 06 '16 at 03:01
  • You can do whatever you want, I'm just trying to understand what post an answer that already exists. thats all. – Dekel Nov 06 '16 at 03:02