I retrieve data from an associative array and i want to insert it in another associative array. anyone can tell me how to do it?

Recommended Answers

OP, you are in "JavaScript / DHTML / AJAX" and javascript doesn't have associative arrays.

Jump to Post

All 4 Replies

By associative array you mean object?
Even if you recieve an associative array from PHP it'll be transformed into a object.

You can just loop one and insert in the other, like this:

var obj1 = {
    'banana': 'banana',
    'apple' : 'apple'
};

var obj2 = {
    'world': 'world',
    'earth': 'earth'
};

// Adds all properties in obj2 into obj1
// Any property that exists in obj1 and obj2 will be overwritten.
for(var prop in obj2) {
    obj1[prop] = obj2[prop];
}

actually i want to add it dynamically. is it possible?

Isn't this dynamic? Any propertie will be added...

You should only create a function to do it.

Or if you use jQuery use the $.extend() method: $.extend(obj1, obj2);

OP, you are in "JavaScript / DHTML / AJAX" and javascript doesn't have associative arrays.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.