如果我在函数中使用json_encode,它会删除一个数组

时间:2016-12-17 22:14:23

标签: php arrays json

我不知道为什么但是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 个答案:

答案 0 :(得分:1)

使用example1传递一个数组: ["inline_keyboard" => [[["text" => "Google", "url" => "http://google.com"]],[["text" => "test", "callback_data" => "test"]]]]

使用example2,您实际上是将两个参数传递给函数inkeyboard。其中一个是[["text" => "Google", "url" => "http://google.com"]],第二个是[["text" => "test", "callback_data" => "test"]]

如果您将example2更改为:

,它将正常工作
$example2 = inkeyboard(array(array("text" => "Google", "url" => "http://google.com"),array(array("text" => "test", "callback_data" => "test"))));