douhuang5331 2016-12-17 22:14
浏览 45
已采纳

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

I don't know why but json_encode output has only an array in my second example.

Here is my codes:

$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;
}

and output is like this:

Example 1 output:

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

Example 2 output:

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

they should be same. what is happening?

  • 写回答

1条回答 默认 最新

  • dongyied24121 2016-12-17 22:22
    关注

    With example1 you pass one array: ["inline_keyboard" => [[["text" => "Google", "url" => "http://google.com"]],[["text" => "test", "callback_data" => "test"]]]]

    With example2, your are actually passing two arguments to the function inkeyboard. One of them being [["text" => "Google", "url" => "http://google.com"]], the second one [["text" => "test", "callback_data" => "test"]].

    It would work correctly, if you change example2 to:

    $example2 = inkeyboard(array(array("text" => "Google", "url" => "http://google.com"),array(array("text" => "test", "callback_data" => "test"))));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 武汉岩海低应变分析软件,导数据库里不显示波形图
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥30 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile