59450

Convert JSON String into variables to echo out

Question:

I have the following JSON string:

[{\"index\":0,\"date\":\" 20120030\",\"title\":\"afsa\"}]

And I need to get out the contents into a variable foreach one.

This is how it is picked up so far...

$json_data_string = $_POST['hidden_event']; // sanitize however $array_data = json_decode($json_data_string); echo $json_data_string;

I need to be able to echo each out. for example:

foreach { echo $date; echo $title; }

Thanks in advance for any help.

Answer1:

I think if you want to use this using jquery you will do like this:-

var recordList = [{\"index\":0,\"date\":\" 20120030\",\"title\":\"afsa\"}] jQuery.each(recordList, function() { alert(this.Name); // For example alert(this.date); // For example });

or like this:-

$.ajax({ type: "POST", url: URL, cache:false, data: values, dataType:'json', success: function(json) { var date = json.date; alert(date); } // end success function });

Answer2:

foreach($array_data as $data) { echo $data->date, PHP_EOL; echo $data->title, PHP_EOL; }

Answer3:

You can use <a href="http://us.php.net/manual/en/function.extract.php" rel="nofollow">extract</a> function on $array_data to get variables.

$array_data = json_decode($json_data_string); extract($array_data); echo $index; echo $date;

Answer4:

Try var_dump (<a href="http://php.net/manual/en/function.var-dump.php" rel="nofollow">http://php.net/manual/en/function.var-dump.php</a>), that will give you idea how $array_data is structured. If you do this

echo '<pre>'; var_dump($array_data); echo '</pre>';

you get even prettier dump. From there it's farily easy to see how to echo variables.

Answer5:

$json_string = $_POST['hidden_event']; $array = json_decode ( $json_data_string); extract ($array); echo $date; echo $title;

Recommend

  • SmartGWT ComboBoxItem get all Records
  • Laravel 5.3 : Passport Implementation - {“error”:“invalid_client”,“message”:“Client authentication f
  • PHP script receives GET instead of POST REQUEST
  • PHP not converting JSON using 'json_decode()'
  • testing a POST using phpunit in laravel 4
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Do I need to reset a Perl hash index?
  • Display images in Django
  • How to render a blob on a canvas element?
  • jQuery .attr() and value
  • How to clear text inside text field when radio button is select
  • Get data from AJAX - How to
  • Seeking advice on Jetty HttpClient Hang
  • Email format validation in mvc3 view
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Why is an OPTIONS request sent to the server?
  • Spring security and special characters
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • jQuery tmpl and DataLink beta
  • R: gsub and capture
  • Circular dependency while pushing http interceptor
  • How to disable jQuery.jplayer autoplay?
  • How to delete a row from a dynamic generate table using jquery?
  • AngularJs get employee from factory
  • Benchmarking RAM performance - UWP and C#
  • Free memory of cv::Mat loaded using FileStorage API
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • using HTMLImports.whenReady not working in chrome
  • How to set the response of a form post action to a iframe source?
  • Memory offsets in inline assembly
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Change div Background jquery
  • Turn off referential integrity in Derby? is it possible?
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize