Home > Net >  JSON array to comma seperate string
JSON array to comma seperate string

Time:11-26

I have the array snippet below.

I would like to get the image urls into a string like so $images = "url,url.....";

I have tried the below but it doesn't work.

$data = $item['media']['images'];
$v62 = implode (", ", $data);

"media": {
  "images": [{
    "href": "https:\/\/m.atcdn.co.uk\/a\/media\/{resize}\/88bef44b719d46c59112ba3f143ec0c3.jpg"
  }, {
    "href": "https:\/\/m.atcdn.co.uk\/a\/media\/{resize}\/17831bddfa1f459c8802fbd2ff66580a.jpg"
  }, {
    "href": "https:\/\/m.atcdn.co.uk\/a\/media\/{resize}\/7c5996ca5cd04f51a7b1b4aff6fa6a5d.jpg"
  }, {
    "href": "https:\/\/m.atcdn.co.uk\/a\/media\/{resize}\/16ecde9a63254b4895f347882e7f7a48.jpg"
  }, {
    "href": "https:\/\/m.atcdn.co.uk\/a\/media\/{resize}\/47c632f4bb33468f95bfc7eec508fc00.jpg"
  }, {
    "href": "https:\/\/m.atcdn.co.uk\/a\/media\/{resize}\/3432255d29e34f28b4c23eb685df607a.jpg"
  }, {
    "href": "https:\/\/m.atcdn.co.uk\/a\/media\/{resize}\/948cdf1b75fb4a638f49504443fd3087.jpg"
  }, {
    "href": "https:\/\/m.atcdn.co.uk\/a\/media\/{resize}\/4b0e4578ad184cbb8e35c57f4b978521.jpg"
  }, {
    "href": "https:\/\/m.atcdn.co.uk\/a\/media\/{resize}\/ad008df7903e4972ba7aaa12a8273540.jpg"
  }, {
    "href": "https:\/\/m.atcdn.co.uk\/a\/media\/{resize}\/ff9fa7b38b904de994a71b0ebbccb7e2.jpg"
  }, {
    "href": "https:\/\/m.atcdn.co.uk\/a\/media\/{resize}\/ca41f72df77946e68573333bcbfa043b.jpg"
  }, {
    "href": "https:\/\/m.atcdn.co.uk\/a\/media\/{resize}\/f63354fdad794e6d926c87aa47ff31cb.jpg"
  }, {
    "href": "https:\/\/m.atcdn.co.uk\/a\/media\/{resize}\/9db909ff57434717af9c6a1a5f810f64.jpg"
  }],

CodePudding user response:

You're not accessing the href element in the nested arrays. You can use array_column() to get the same key of each element of an array.

$v62 = implode(", ", array_column($data, "href"));
  • Related