2016-11-06 90 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

相關問題