Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

How do I extract data from JSON with PHP?

I have
Code
$images = json_decode('{"small": "path-to-small", "large": "path-to-large"}');

When I want to print the path of image small or image large I'm using this:
Code
print $images->{'small'};
print $images->{'large'};

But how to print the path in echo, for example:

Code
echo '<a href="LARGE HERE" data-lightbox="mygallery" title=""><img src="SMALL HERE" alt=""></a>';

I think you get the point!
Thank you!
In
Code
<?php ... ?>
tags. Also, shouldn't you be using a foreach?

[Image: K2c6AfR.png]
(Aug 10, 2018 07:58 AM)Don Vito Corleone Wrote:  In
Code
<?php ... ?>
tags. Also, shouldn't you be using a foreach?

Of course I have <?php ?> and foreach but that's not my question.
I'm asking for the syntax in <a> and <img> tags for outputting the paths from json_decode in PHP.
(Aug 10, 2018 08:13 AM)pr0slyte.. Wrote:  Of course I have <?php ?> and foreach but that's not my question.
I'm asking for the syntax in <a> and <img> tags for outputting the paths from json_decode in PHP.

Put the results in variables like you said you have done, and the input it in the tags like the code below. If this is also not what you meant than probably someone else needs to help us out.

Code
a href="<?php (php code) ?>"

For image src it is the same I believe.

[Image: K2c6AfR.png]
(Aug 10, 2018 08:33 AM)Don Vito Corleone Wrote:  Put the results in variables like you said you have done, and the input it in the tags like the code below. If this is also not what you meant than probably someone else needs to help us out.

Code
a href="<?php (php code) ?>"

For image src it is the same I believe.

I have tried this already. Doesn't working. 😶
(Aug 10, 2018 08:38 AM)pr0slyte.. Wrote:  I have tried this already. Doesn't working. 😶

Why doesn't it work? Your server should execute any PHP before it responds with the page to the client. What's it doing?
PHP Code

<?php 
$j 
= json_decode('{"small": "path-to-small", "large": "path-to-large"}');
foreach(
$j as $item)
{
 
?>

<a href=<?=$item->large;?> data-lightbox="mygallery" title=""><img src=<?=$item->small;?> alt=""></a>; 
  <?php
}
?>
(Aug 10, 2018 07:31 AM)pr0slyte.. Wrote:  I have
Code
$images = json_decode('{"small": "path-to-small", "large": "path-to-large"}');

When I want to print the path of image small or image large I'm using this:
Code
print $images->{'small'};
print $images->{'large'};

But how to print the path in echo, for example:

Code
echo '<a href="LARGE HERE" data-lightbox="mygallery" title=""><img src="SMALL HERE" alt=""></a>';

I think you get the point!
Thank you!

You're decoding it correctly. What you're trying to do is string concatenation though. PHP supports this in a number of ways to do this.

Concatenation Operator
PHP Code

<?php

$images 
= json_decode('{"small": "path-to-small", "large": "path-to-large"}');
echo 
'Path to small: ' . $images->small 

String Interpolation - Simple
PHP Code

<?php

$images 
= json_decode('{"small": "path-to-small", "large": "path-to-large"}');
echo 
"Path to small: $images->small"; 

String Interpolation - Complex
PHP Code

<?php

$images 
= json_decode('{"small": "path-to-small", "large": "path-to-large"}');
echo 
"Path to small: {$images->small}"; 

There are others, but these three will handle most use cases. Concatenation is what you're likely going to use in your specific example. At least as it's structured now. When you need to content with quotations in your output you need to decide between concatenation and interpolation. For interpolation to work you need to use double quotes; PHP won't interpret what is inside a single quote string literal. But if you switch to using double quote strings, you need to escape any occurrence of double quotes in the output.

Another alternative, though overkill for your example, is the use of functions such as printf() or sprintf(). They are basically identical to each other; the difference being that printf() will immediately output the formatted string. You can store the output of sprintf() to a variable.
<Disease />
It works! Thanks you all! :)


Possibly Related Threads...
Thread / Author Replies Views Last Post
by HackersMar 14, 2025 02:08 PM 6 279 Mar 14, 2025 02:08 PM
Last Post: Zher0
[Javascript]  by ∑clipse™May 13, 2021 08:30 AM 9 312 May 13, 2021 08:30 AM
Last Post: grady
[Javascript]  by ApricusJan 12, 2021 02:30 PM 2 143 Jan 12, 2021 02:30 PM
Last Post: techies