![]() |
|
[PHP] How do I extract data from JSON with PHP? - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183) +---- Thread: [PHP] How do I extract data from JSON with PHP? (/showthread.php?tid=5872203) |
How do I extract data from JSON with PHP? - pr0slyte.. - Aug 10, 2018 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'};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! RE: How do I extract data from JSON with PHP? - Don Vito Corleone - Aug 10, 2018 In Code <?php ... ?>RE: How do I extract data from JSON with PHP? - pr0slyte.. - Aug 10, 2018 (Aug 10, 2018 07:58 AM)Don Vito Corleone Wrote: In 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. RE: How do I extract data from JSON with PHP? - Don Vito Corleone - Aug 10, 2018 (Aug 10, 2018 08:13 AM)pr0slyte.. Wrote: Of course I have <?php ?> and foreach but that's not my question. 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. RE: How do I extract data from JSON with PHP? - pr0slyte.. - Aug 10, 2018 (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. I have tried this already. Doesn't working. 😶 RE: How do I extract data from JSON with PHP? - TyrantKingBen - Aug 10, 2018 (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? RE: How do I extract data from JSON with PHP? - Charger - Aug 10, 2018 PHP Code RE: How do I extract data from JSON with PHP? - Disease - Aug 10, 2018 (Aug 10, 2018 07:31 AM)pr0slyte.. Wrote: I have 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 String Interpolation - Simple PHP Code String Interpolation - Complex PHP Code 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. RE: How do I extract data from JSON with PHP? - pr0slyte.. - Aug 11, 2018 It works! Thanks you all! :) |