2016-11-06 59 views
0

当我尝试打印从该函数返回的数组时,我得到一个空白屏幕。array_combine为什么不返回数组?

我的阵列$terms$definitions都是相同的长度,他们存在之前和之后我呼吁make_associative_array()

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

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

$方面:

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. 
) 

我使用PHP 27年6月5日

回答

1

在你的情况 - array_combine回报NULL,因为这两个$terms & $definitions一个在make_associative_array的范围内重新为null。

您可以使它们的全球:

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

或者将它们传递给函数:

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

反正 - 我真的建议你打开错误:
http://sandbox.onlinephpfunctions.com/code/40cfd2d197aebd4d935c793c1ea662cab50ce8b1

1

您必须将参数传递给功能

<?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); 

输出将是

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. 
) 
+0

没有必要重复一个已经存在的答案(你可以投票现有的答案,你知道...) – Dekel

+0

@Dekel。我没有重复答案。我给出答案,通过在localhost中执行hist代码给出答案。如果我们在localhost中exicute并发布它需要时间,当我们发布后 – iCoders

+0

需要时间。您的回答正是我已经写过的。你能解释一下这些差异吗? – Dekel

相关问题