1

I have a following code:

$string = '{"tracking_url":{"11":{"affiliate":"OMG","url_part1":<<some url>>,"url_part2":<<some url>>"}}}';
$points = json_decode( $string, true );

How do I access url_part1 and url_part2?

4
  • 3
    Should have {} around all of that, no? Commented Mar 15, 2013 at 19:49
  • Are you getting an error? php.net/manual/en/function.json-decode.php has a pretty straight forward example (#2). Commented Mar 15, 2013 at 19:50
  • var_dump(json_decode($json)) will show you what needs to be done. Commented Mar 15, 2013 at 19:52
  • Sorry for the typo. Actually i presented just a part of JSON so missed the 1st curly. I ab able to decode without any error but dont know how to echo it Commented Mar 15, 2013 at 20:03

1 Answer 1

5

Your given string is not valid JSON.

I assume you have something like this

{ "tracking_url":{"11":{"affiliate":"OMG","url_part1":<<some url>>,"url_part2":<<some url>>"}} }

Then you can access the properties after parsing by

$obj = json_decode( '{ "tracking_url":{"11":{"affiliate":"OMG","url_part1":<<some url>>,"url_part2":<<some url>>"}} }', true );

I forgot the output part. As is set the second argument of json_decode() to true, the result is an associative array and you can access/output it like this:

echo $obj['tracking_url']['11']['url_part1'];
echo $obj['tracking_url']['11']['url_part2'];
Sign up to request clarification or add additional context in comments.

6 Comments

$obj->{'url_part1'} and $obj->{'url_part2'} are then your variables.
@Sirko sorry for the unstructured question. I am able to decode it but dont know how to echo url_part1 and url_part2
@bozdoz How would i echo them??
Write echo in front. @user2129794
Sorry, missed some of the path to the object value: should be: echo $points->{'tracking_url'}->{'11'}->{'url_part1'};
|