dongyang4615 2019-07-26 10:36
浏览 81
已采纳

Json以[并且当使用json_encode时以“magento2使用php7.1开头”开头

I want my json to start with { but if using json_encode it is getting converted into string, I am using php7.1 on ubuntu and working on magento 2.3

This is what I am getting with the below code, I don't want '['

[
    {
        "success": "true",
        "data": {
            "mainimages": [
                {

Here is my code

$response = array(
    array(
        "success" => "true",
        "data" => $alldata,
        "newarrivalheading" => "NEW ARRIVALS",
        "instagramheading" => "CELEBS IN LULU",
        "specialpriceheading" => "SPECIAL PRICES",
        "editorwishlistheading" => "EDITOR'S WISHLIST",
        "stylehighlightheading" => "STYLE HIGHLIGHTS",
        "styletagline" => "#Looks to swipe right",
        "newarrivalindex" => 3,
        "instagramindex" => 9,
        "editorwishlistviewall" => "",
        "sliderimage" => $sliderimage
    )
);      
return $response;

This is what I want

 {
        "success": "true",
        "data": {
            "mainimages": [
                {
  • 写回答

1条回答 默认 最新

  • dounie0889 2019-07-26 10:52
    关注

    So remove the unnecessary outer array like so

    $alldata = [1,2,3,4];
    $sliderimage = ['xz.jpg','ab.png'];
    
    $response = array(
                   "success" => "true",
                   "data" => $alldata,
                   "newarrivalheading" => "NEW ARRIVALS",
                   "instagramheading" => "CELEBS IN LULU",
                   "specialpriceheading" => "SPECIAL PRICES",
                   "editorwishlistheading" => "EDITOR'S WISHLIST",
                   "stylehighlightheading" => "STYLE HIGHLIGHTS",
                   "styletagline" => "#Looks to swipe right",
                   "newarrivalindex" => 3,
                   "instagramindex" => 9,
                   "editorwishlistviewall" => "",
                   "sliderimage" => $sliderimage
            );
    echo json_encode($response);
    

    RESULT

        {
        "success": "true",
        "data": [
            1,
            2,
            3,
            4
        ],
        "newarrivalheading": "NEW ARRIVALS",
        "instagramheading": "CELEBS IN LULU",
        "specialpriceheading": "SPECIAL PRICES",
        "editorwishlistheading": "EDITOR'S WISHLIST",
        "stylehighlightheading": "STYLE HIGHLIGHTS",
        "styletagline": "#Looks to swipe right",
        "newarrivalindex": 3,
        "instagramindex": 9,
        "editorwishlistviewall": "",
        "sliderimage": [
            "xz.jpg",
            "ab.png"
        ]
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,