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": [
                {

图片转代码服务由CSDN问答提供 功能建议

我希望我的json以{但如果使用json_encode将其转换为字符串,我使用的是php7。 1在ubuntu上工作并使用magento 2.3

这是我用以下代码得到的,我不想要'['

 < 代码> [
 {
“成功”:“真”,
“数据”:{
“mainimages”:[
 {
   
 
 

这是我的代码

  $ response = array(
 array(
“success”=&gt;“true”,
“data”=&gt; $ alldata,  
“newarrivalheading”=&gt;“NEW ARRIVALS”,
“”instagramheading“=&gt;”CELEBS IN LULU“,
”specialpriceheading“=&gt;”特殊价格“,
”editorwishlistheading“=&gt;”EDITOR'S  WISHLIST“,
”stylehighlightheading“=&gt;”STYLE HIGHLIGHTS“,
”styletagline“=&gt;”#Looks to rightipe right“,
”newarrivalindex“=&gt; 3,
”instagramindex“=&gt;  9,
“editorwishlistviewall”=&gt;“”,
  “sliderimage”=&gt;  $ sliderimage 
)
);  
return $ response; 
   
 
 

这就是我想要的

  {
“”success“:”true  “,
”数据“:{
”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"
        ]
    }
    
    打赏 评论

相关推荐 更多相似问题