-2

My echo output is below - NOTE: This comes from a Joomla module echo statement - Output of a repeatable field.

$params->get('star_slides');

{"Field1":["/demo/slide1.jpg","/demo/slide2.jpg"],"Field2":["Content 1","Content 2"],"Field 3":["Content 3","Content 4"]}

My goal is to extract the individual values of the fields

Set 1 - Values of the first field set.

/demo/slide1.jpg 
Content 1 
Content 3

How can I do this using php?

Kal
  • 1,656
  • 4
  • 26
  • 41
  • 2
    possible duplicate of [JSON to PHP Associative array](http://stackoverflow.com/questions/3073205/json-to-php-associative-array) – Ben Jan 18 '15 at 00:56
  • Steve - This is not a duplicate - This comes from a Module parameter echo statement. – Kal Jan 18 '15 at 01:03

1 Answers1

0

You have to parse JSON string to object or array, then you can access it's values. Like this:

<?php
$json = '{"Field1":["/demo/slide1.jpg","/demo/slide2.jpg"],"Field2":["Content 1","Content 2"],"Field 3":["Content 3","Content 4"]}';
$parsed = json_decode($json, true);
echo $parsed['Field1'][0];
echo "\n";
echo $parsed['Field2'][0];
echo "\n";
echo $parsed['Field 3'][0];
echo "\n";
?>

It outputs:

/demo/slide1.jpg
Content 1
Content 3

Link to demo on Codepad.

Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89
  • Not sure who is voting down your answer - Your answer works perfectly. – Kal Jan 18 '15 at 01:04
  • Strange rules here - I am not allowed to Vote Up. I am relatively new here and dont have enough points to vote. – Kal Jan 18 '15 at 01:07