Question

I created an PHP array with mysql_fetch_array from my SQL database. I retrieved it to JavaScript with ajax and jQuery.

My problem is that JavaScript created two array, one simple and one associative, instead of PHP where both are in one.

When I update a cell by accessing it with index value myArray[0], the associative value of the same cell myArray['col1'] is not updated.

Is there a way to get the key from a stand alone cell from index value so I can update both arrays at the same time?

Update:

Here is the PHP code wo generate the array (XY).

 $query = mysql_query("SELECT * FROM details_moteurs ORDER BY m_no_inventaire ASC")
    $i=0;
    while ($infosMoteurs = mysql_fetch_array($query)) {
        $arrayMoteur[$i] = $infosMoteurs;
        $i++;
    }

The firt column of my table details_moteurs is m_id.

// javascript code
Alert($arrayMoteur[0][0]) // give '1524'
Alert($arrayMoteur[0]['m_id']) // give me  '1524'

But if i want to update a cell of my bi-dimensional array with:

$arrayMoteur[0][0] = 'xyz';  
alert($arrayMoteur[0]['m_id']) // give me '1524'

And same:

   $arrayMoteur[0]['m_id'] = 'xyz';  
   alert($arrayMoteur[0][0]) // give me '1524'

And my problem is that I used a loop:

 for(x = 0;x < arraySize;x++){
       for(y = 0;y < lineSize;x++){
            if(something){
                 $arrayMoteur[x][y] = 'xyz';
            }
        }
    }

================================================================

Okay i fixed it.

I replaced mysql_fetch_array by mysql_fetch_assoc to get only one array, and I corrected my loops with:

for(var x in array)
       for(var y in array[x]){
            if(something){
                 $arrayMoteur[x][y] = 'xyz';
            }
        }
    }
Was it helpful?

Solution

Javascript does not create two separate arrays -- it simply creates an object where you can reference it's properties via an associative array syntax. Try the following out in your console:

var test = [];
test[0] = "value0";
test["col1"] = "value1";
test["col2"] = "value2";
console.log(test.length);

If you run the above code, you will see that the length of the array is actually just 1. The values for keys "col1" and "col2" are added as properties to the array object, not as entries in the array. Assuming that all the entries that you added to your javascript array were strings (eg., all the php generated keys were strings), then you can easily loop through all the keys in order using this:

for (key in test) { 
    console.log( test[key] ); 
}

The above code in our example would print out:

value0
value1
value2

Hope this clears up what is happening to your javascript array/object.

Update: If you need/want the 0..n elements to match your properties you could execute this after creating your initial javascript object with the associative references:

var test = [];
test["col1"] = "value1";
test["col2"] = "value2";

var i = 0;
for (key in test) { 
    test[i] = test[key];
    i++; 
}

In our example above, you would actually end up with 4 keys. Running this code:

for (key in test) { 
    console.log( "[" + key + "] --> " + test[key] ); 
}

outputs:

[0] --> value1
[1] --> value2
[col1] --> value1
[col2] --> value2

However, you could also just pull the two associative values using the index count like this:

for (var j = 0; j < test.length; j++) {
    console.log( "[" + j + "] --> " + test[j] );
}

This would print just the 2 values like this:

[0] --> value1
[1] --> value2

Again, hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top