Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Below i have given my code which is a javascript function which calls a php file and gets json data .... all things are perfect yet but i am getting problem when i want to adda data to a multidimensional array .. sorry for this noob question.. i don't know it can be done or not.

what i want to do

  1. i want to retrieve data on ajax call and save this data to array
  2. so i dont need to call again if i need data again
  3. so i want to store data in multidimensional array which will have two keys
  4. first key will be length iof array
  5. and second will be dataType of my data which can be "name", "id", "description"
  6. is this right way to do this
  7. if it is not then what should i try ...? HELP ME...!!!
  8. or can i assign full jsondata object data got from ajax call success to an array

Newly added requirements of my question Its associative array like my php array of data is like

$data[0]["id"] = 1;
$data[0]["name"] = "spomething";
$data[0]["descriptoon"] = "spomething";
$data[0]["image"] = "spomething";

//for second item
$data[1]["id"] = 1;
$data[1]["name"] = "spomething";
$data[1]["descriptoon"] = "spomething";
$data[1]["image"] = "spomething";

so i want to create same array for javascript... for every item i fetch... from ajax../ and include into javascript array...

    $('.searchButton').bind("mouseover", function() {

        var data = $(this).parents('.item').attr('id');   
        var type = $(this).parents('.tab-pane').attr('id'); 
        // dataArray will be global javascript variable because of its needdss... it will be outside the function
        var dataArray = new Array();

        $.ajax({
            type: 'GET',
            url: '<?php echo $this->createUrl("dining/AjaxItemDetailsCall") ?>/data/'+data,
            dataType: 'json',
            async: false,
            success: function(data){  
                var dataArrayLength = dataArray.length;


             dataArray.push({dataArrayLength:data});

         console.log(dataArray[0]);                                              

            }
        });       
    });

now i change my code on thebasis of your suggestion i can push data object in array via push fn ... now how do i call inside data from that object ....!

Object {dataArrayLength: Object}
dataArrayLength: Object
description: "World famous roasted Chicken, avacado, fresh mushrooms, red onions.       Mozzarella cheese and our secret sauce."
id: "11"
images: "/htdocs/kiosk/images/dining/items/pizzas/11.jpg"
name: "Bella Notte Signature Pizza"
price: "4.50"
raters: "10"
ratings: "1"
__proto__: Object
__proto__: Object

this is the output of console.log(dataArray[0]) ... so now i want to get "id" , "name", "description" from it how can i do this...????/

MY SOLUTION TO PROBLEM

 success:function(data){    
var dataArrayLength = dataArray.length;

dataArray.push(data);
dataArray.forEach(function(object,index){
console.log(object.id);
console.log(object.name);
console.log(object.description);
console.log(object.image);
console.log(object.ratings);
});

and this is what i want to do ...

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Try this:

$.ajax({
type:'GET',
url:'<?php echo $this->createUrl("dining/AjaxItemDetailsCall") ?>/data/'+data,
dataType: 'json',
async: false,
success:function(data){
  dataArray = []; // empty it before pushing data into it...
  dataArray.push(data);
}
});       

I believe you can directly push the data object into the dataArray. and while you access this simple access it like dataArray[i].id, dataArray[i].name and so on for all the keys of data.

share|improve this answer
    
every time i only call one item data but it will be got incremented when i call for another item... here i have said that i will check dataArray before ajax call if data exist it will not call if not for the item it will call ajax ... My point is json data will be for only one item .. but when i cvall again it wiill be incremented... –  Jaimin MosLake Dec 13 '13 at 5:23
    
you mean if you already have data in your dataArray then you dont want to make an AJAX call , am I taking you rigth ? –  robieee Dec 13 '13 at 5:34
    
yup but .. thanks .. i figured it out ... look question after some time i will write my solution for it.... Thanks buddy for your help.... –  Jaimin MosLake Dec 13 '13 at 5:37
    
please check my edit –  robieee Dec 13 '13 at 5:44

I'm not quite sure i understand, but cant you simply do:

$.ajax({
    type:'GET',
    url:'<?php echo $this->createUrl("dining/AjaxItemDetailsCall") ?>/data/'+data,
    dataType: 'json',
    async: false,
    success:function(data){ 
        var dataArrayLength = dataArray.length;
        dataArray.push({'id':data.id,'name':data.name,'image':data.image,'description':data.description});
    }
       });            

 });

Or even:

dataArray.push(data);

I havent tested this so consider it pseudo code but i belive that the push method is what you are looking for?

share|improve this answer
    
yup .. but i want to store data for morethan one item... its like facebook popover for persons name .. when you mouse over the name you got data of person image name and all that .... they save that data to something when you mouseover on another name you got data for that person also ... that data also got saved ... so like that i want to save data for all the items on which user mouseover ... so whenevre user mouseover on the existing data ... it will not call an ajax function.. –  Jaimin MosLake Dec 12 '13 at 14:29
    
Array.push just adds the object as the next element in the array, so you can add as many objects as you want (the limits of JS not-withstanding). –  Andy Dec 12 '13 at 14:44
    
Andy itrs very helpfull but i want to crteate something else.. see my question again i have edited it... and added some proper thing what i want to do ... –  Jaimin MosLake Dec 13 '13 at 4:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.