Ahmad Asjad Ahmad Asjad - 10 months ago 41
PHP Question

How to put data into multi dimensional array in js inside the loop

How can we put data into multi dimensional array or json within a loop.
I know that it's possible to store multi dimensional data at one time, but I want it inside the loop as I have described in the code.

var sub_cat_checked_val = [];
sub_cat_checked.each(function (index) {
var sub_cat_id = jQuery(this).attr('name').replace('subcategory_id_', '').replace('[]', '');
sub_cat_checked_val[sub_cat_id][index] = index:jQuery(this).val();

As it's possible in php like

Answer Source

sub_cat_checked_val[sub_cat_id] needs to be defined as an array, so add the lines:

if (!sub_cat_checked_val[sub_cat_id]) {
    sub_cat_checked_val[sub_cat_id] = [];

to define the value as an array if it does not exist.