1

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.

2
  • if u want to do that using jquery ajax and php i will help u out little.. Commented May 14, 2012 at 10:48
  • Great. I just need the data into a variable for each statement. Commented May 14, 2012 at 10:50

5 Answers 5

2

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
                        });  
Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

try changing the lines to echo $data->date . "\n"; and echo $data->title . "\n";
h2ooooooo is right about -> instead of => (i corrected my typo) but you should prefer PHP_EOL over "\n"
@mathroc PHP_EOL is useful for log files or command line integration, but in this case where you're outputting it to the user, it wouldn't make a difference, as no matter whether you use \n or \r\n (dependent on whether the server is Windows or *nix), it would only be visible to the user unless a <pre> tag was used, and if the <pre> tag was used then both \n and \r\n will be substituted with a single new line (at least on my Chrome 18). Still, you're right, you might as well use PHP_EOL, but just remember that it won't make any difference in the case of output.
absolutly. i'm always using (and advertising) PHP_EOL just because then you don't have to think about it
1

You can use extract function on $array_data to get variables.

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

2 Comments

Cool, how do i go about that?
i've added example to my answer
0

Try var_dump (http://php.net/manual/en/function.var-dump.php), 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.

4 Comments

When i do that. I just get NULL
That means your JSONRequest is not sending anything, or parameter is not named as you mention there (hidden_event). It's tricky to sort out what happens in JSON requests, try var_dumping whole $_POST (and maybe also $_GET). Are you sure that you are using JSON POST method, and not GET? If you have firefox, try using firebug, that gives you idea what happens with these "hidden" JSON requests.
from the manual : "NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit." you should try to var_dump($json_data_string); to see if it's well formated
TBH I didnt checked how json_decode behaves, and now thinking if $_POST['hidden_event'] would not exists you would find your problem earlier, so I stand corrected. Try what mathroc suggested, and you probably find your problem there. When ever I cannot use normal debug tools, I find var_dump very easy way to see what is going on.
0
$json_string = $_POST['hidden_event'];
$array = json_decode ( $json_data_string);

extract ($array);

echo $date;
echo $title;

2 Comments

hmm that doesnt appear to work. Infact, i cannot echo out $array after it's been through json_decode?
No, but. even if i do: echo $array_data with what i have uptop in the question it doesnt work :s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.