0

我希望我的 json 以 { 开头,但如果使用 json_encode 它会转换为字符串,我在 ubuntu 上使用 php7.1 并使用 magento 2.3

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

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

这是我的代码

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

这就是我要的

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

1 回答 1

1

所以像这样删除不必要的外部数组

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

结果

    {
    "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"
    ]
}
于 2019-07-26T10:52:04.797 回答