2016-12-17 26 views
0

我不知道爲什麼,但在我的第二個例子中,json_encode輸出只有一個數組。如果我在函數中使用json_encode,它將刪除一個數組

這是我的代碼:

$example1 = json_encode(array("inline_keyboard" => array(array(array("text" => "Google", "url" => "http://google.com")),array(array("text" => "test", "callback_data" => "test"))))); 

$example2 = inkeyboard(array(array("text" => "Google", "url" => "http://google.com")),array(array("text" => "test", "callback_data" => "test"))); 


function inkeyboard($array){ 
    $keyboard = array($array); 

    $resp  = array("inline_keyboard" => $keyboard); 
    $reply  = json_encode($resp); 

    return $reply; 
} 

和輸出是這樣的:

實施例1個輸出:

{"inline_keyboard":[[{"text":"Google","url":"http:\/\/google.com"}],[{"text":"test","callback_data":"test"}]]} 

實施例2的輸出:

{"inline_keyboard":[[{"text":"Google","url":"http:\/\/google.com"}]]} 

它們應該是相同的。發生什麼事?

回答

1

有了例1你傳遞一個數組: ["inline_keyboard" => [[["text" => "Google", "url" => "http://google.com"]],[["text" => "test", "callback_data" => "test"]]]]

隨着例題,你實際上是傳遞兩個函數的自變量inkeyboard。其中一個是[["text" => "Google", "url" => "http://google.com"]],第二個是[["text" => "test", "callback_data" => "test"]]

這將正常工作,如果你改變例2到:

$example2 = inkeyboard(array(array("text" => "Google", "url" => "http://google.com"),array(array("text" => "test", "callback_data" => "test")))); 
+0

OMG,我忘了在該函數的第二個參數的情況。數組讓我頭腦混亂。非常感謝。 –

相關問題