Jump to content
jploch

[SOLVED] Saving data as json

Recommended Posts

Hey there!
For a module Iam working on I need an easy way to save some data as json (the data does not need to be searchable). I have trouble to save a json array in a textarea and then loop over the array with php.
I don't understand why this is not working (Iam not experienced with json).

JS to save the json in textarea:

var s_data = [];

$('.pgrid-item').each(function () {

    // JSON Test Storing data:
    myObj = {
      "item": [{
        id: "id-test",
        name: "test"
        }]
    };
    s_data.push(myObj);

  });

  myJSON = JSON.stringify(s_data);
  $('#Inputfield_pgrid_style_base').val(myJSON);


php code to decode the string and loop over the array:

$someArray=json_decode($page->pgrid_style_base, true);

foreach ($someArray->item as $value) {
    echo $value->id;
  }

 

Share this post


Link to post
Share on other sites

I keep answering my own questions. Sorry this was a noob mistake. Its working fine with this php loop: 

$someArray=json_decode($page->pgrid_style_base, true);

  foreach ($someArray as $object) {
    foreach ($object['item'] as $item) {
      echo $item['id'];
    }
  }

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...