首页 > 解决方案 > 在 laravel 中附加 JSON 数组的 url

问题描述

我的 MySQL 表列中有一个 JSON 字段,其中包含一个带有部分图像 URL 的 JSON 数组。

"images": [
            {
              "images": {
                 "original": "/storage/uploads/1.png",
                 "300": "/storage/uploads/300.1.png",
                 "600": "/storage/uploads/600.1.png",
                 "900": "/storage/uploads/900.1.png"
                    },
               "thumb": "/storage/uploads/300.1.png"
            },
            {
               "images": {
                  "original": "/storage/uploads/2.png",
                  "300": "/storage/uploads/300.2.png",
                  "600": "/storage/uploads/600.2.png",
                  "900": "/storage/uploads/900.2.png"
                    },
                "thumb": "/storage/uploads/300.2.png"
             },
             {},
           ]

我想通过为数组的每个值附加一个基本 URL 来获取数组。

    "images": [
            {
              "images": {
                 "original": "http://localhost:8000/storage/uploads/1.png",
                 "300": "http://localhost:8000/storage/uploads/300.1.png",
                 "600": "http://localhost:8000/storage/uploads/600.1.png",
                 "900": "http://localhost:8000/storage/uploads/900.1.png"
                    },
               "thumb": "http://localhost:8000/storage/uploads/300.1.png"
            },
            {
               "images": {
                  "original": "http://localhost:8000/storage/uploads/2.png",
                  "300": "http://localhost:8000/storage/uploads/300.2.png",
                  "600": "http://localhost:8000/storage/uploads/600.2.png",
                  "900": "http://localhost:8000/storage/uploads/900.2.png"
                    },
                "thumb": "http://localhost:8000/storage/uploads/300.2.png"
             },
             {},
           ]

我已经尝试过像下面的代码那样使用 Collections 函数。但是没有成功。

'images' => collect($item->images)->map(function ($image) {
                return url($image);
             })->all(),

标签: phplaraveleloquent

解决方案


您可以使用嵌套的 foreach 来解决这个问题

$json = json_decode($item->images, true); // I'm using true to convert the json to array

$images = [];

foreach ($json as $index => $array) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            foreach ($value as $path_key => $path) {
                $images[$index][$key][$path_key] = url($value[$path_key]);
            }
        } else {
            $images[$index][$key] = url($value);
        }
    }
}

如果dd($images);你会得到

array:2 [▼
  0 => array:2 [▼
    "images" => array:4 [▼
      "original" => "http://localhost:8000/storage/uploads/1.png"
      300 => "http://localhost:8000/storage/uploads/300.1.png"
      600 => "http://localhost:8000/storage/uploads/600.1.png"
      900 => "http://localhost:8000/storage/uploads/900.1.png"
    ]
    "thumb" => "http://localhost:8000/storage/uploads/300.1.png"
  ]
  1 => array:2 [▼
    "images" => array:4 [▼
      "original" => "http://localhost:8000/storage/uploads/2.png"
      300 => "http://localhost:8000/storage/uploads/300.2.png"
      600 => "http://localhost:8000/storage/uploads/600.2.png"
      900 => "http://localhost:8000/storage/uploads/900.2.png"
    ]
    "thumb" => "http://localhost:8000/storage/uploads/300.2.png"
  ]
]

推荐阅读