1

I return in a ajax call a multidimensional array and I need to get all values

PhP

public function ExampleAction()
{

 $arr = array(array("title" => 'Example Title 1',"description" => "Morbi leo risus, porta ac consectetur ac","datetime"=>new \DateTime("tomorrow")),
              array("title" => 'Example Title 2',"description" => "Morbi leo risus, porta ac consectetur ac","datetime"=>new \DateTime("today"))
    );
    return new JsonResponse(array(
        'data' =>$arr,
        'success' => true), 200);
}

For simple array I get the values like this in Javascript:

$.ajax({
 type: 'POST',
 url: Routing.generate('example'),
 data: {},
 dataType: 'json',
 success: function(response) {
   for (var key in response.data) {

     title= response.data[key]; 
     description= response.data[key];
     datetime= response.data[key]; 

   },          
  }
})

But I do not know how to get it for multidimensional arrays. I have tried several ways and I have not succeeded.

I appreciate your help in advance.

Edited--------------------------Initial problem------------------------------

I needed to obtain one object like this:

$.ajax({
type: 'POST',
url: Routing.generate('example'),
data: {},
dataType: 'json',
success: function(response) {

  $('#calendar').eCalendar({
    ...
    events: [{
      title: 'Example Title 1', 
      description: 'Description 1. Without link', 
      datetime: new Date(2016, 11, 30) 
    },
    {
      title: 'Example Title 2', 
      description: 'Description 2.', 
      datetime: new Date(2016, 10, 23), 
    }]
  });
}
})

So I put the following:

$.ajax({
type: 'POST',
url: Routing.generate('example'),
data: {},
dataType: 'json',
success: function(response) {

  $('#calendar').eCalendar({
    ....
    events: response.data,
    ....

  });
}
})

But this gave me the following error:

  TypeError: d.getDate is not a function
  if (d.getDate() == day && d.getMonth() == dMonth && d.getFullYear() == dYear) {...

I guess it's because of the way to get the date in php that does not give the same result as the one indicated in javascript. So I wanted to get the data with the ajax call and assign it manually, but I was assigning it to the object, so I was doing it wrong. How can I solve this problem?

Joseph
  • 859
  • 2
  • 13
  • 36
  • You may find it useful to use console.log(JSON.stringify(response)) as the first statement in your success function. This will show you the structure of the JSON in the console (F12 in the browser then select console tab). From that you will get more of an understanding of how the data is mapped and how to get what you need. – Vanquished Wombat Nov 30 '16 at 18:30
  • If you do need to iterate through the entire object you can use some of the answers here http://stackoverflow.com/questions/8312459/iterate-through-object-properties. – Vanquished Wombat Nov 30 '16 at 18:34
  • Hello @VanquishedWombat , I already saw the structure of the ajax response, the problem is that I do not know how to access the values of the inner arrays. – Joseph Nov 30 '16 at 18:51
  • This is the structure: `{"data":[{"title":"Example Title 1","description":"Morbi leo risus, porta ac consectetur ac.","datetime":{"date":"2016-11-30 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"}},{"title":"Example Title 2","description":"Morbi leo risus, porta ac consectetur ac.","datetime":{"date":"2016-12-30 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"}}],"success":true}` – Joseph Nov 30 '16 at 18:54

2 Answers2

2

Actually you don't have a multi-dimensional array. You have an array of objects which have a second level object. But I see why you would think so. Try this

 success: function(response) {
   for (var i = 0; i < response.data.length; i = i + 1) {

     title = response.data[i].title; 
     description = response.data[i].description;
     datetime = response.data[i].datetime; 
     date = datetime.date
     timezone_type = datetime.timezone_type
     timezone = datetime.timezone

     console.log('timezone=' + timezone)

   },   

The datetime object is the trickier but not really hard once you see it done.

EDIT: Now I know you are looking at passing your data into the calandar plugin I can see your problem is related to the date being a string in the JSON format but that the d.getDate() error tells us that it is expecting a date type. You can make a date from a string by using new Date(stringval)

 success: function(response) {
   for (var i = 0; i < response.data.length; i = i + 1) {
    var myDate = new Date(response.data[i].datetime.date)
    response.data[i].date = myDate
    }

  $('#calendar').eCalendar({
    ...
    events: response.data

   }, 

This code walks the data array and makes a javascript date from the datetime.date value then replaces the date with that.

However this code does not use your timezone details - you would need to look at the plugin site docs for assistance on that point. See https://github.com/themouette/jquery-week-calendar/wiki

EDIT: Regarding the date format - you need to use a prescribed date format if you want to use new Date() without chopping the string about. Your format is:

"date":"2016-12-30 00:00:00.000000"

I think this may be an invalid format. I believe the correct version of a date and time would be

'"date":"2016-12-30T00:00:00.000"'

Note the T before the time and miliseconds is 3 digits only. I believe there is a javascript date format that includes timezones which you may need. Asking another question on that specific subject would get you a better result than adding it here.

Vanquished Wombat
  • 9,075
  • 5
  • 28
  • 67
  • That was the idea I had initially, but I realized that I was using it incorrectly inside an object. Thank you. – Joseph Nov 30 '16 at 19:22
  • Hi again @VanquishedWombat, I added an explanation of my initial problem and I would need your help, to see how I can pass the values directly from the ajax response to the function that needs them. – Joseph Nov 30 '16 at 19:46
  • Mi problem is like this http://stackoverflow.com/questions/6680878/how-to-retrieve-info-from-database-using-ajax-json-jquery-week-calendar – Joseph Nov 30 '16 at 20:23
  • The last thing I think is what solves my problem @VanquishedWombat, but the code 'new Date(response.data[i].datetime.date);' shows me invalid date. – Joseph Nov 30 '16 at 22:45
0

Use Objects here instead.
Put all the data you wish to return in one object, and you will not have a problem passing the data. When you get it back, you can then manipulate your data.

return new JsonResponse({ 'data': $arr, 'success' :true} , 200);

Daryl
  • 28
  • 8