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.

I am having trouble converting from a PHP array to a Javascript array and then accessing the value. I have tried JSON encoding and decoding.

PHP:

$simpleArray= [];   
$childProducts = Mage::getModel('catalog/product_type_configurable')
    ->getUsedProducts(null,$_product);   
foreach($childProducts as $child) { //cycle through simple products to find applicable
    $simpleArray[$child->getVendor()][$child->getColor()] = $child->getPrice();
    var_dump ($simpleArray);
}

Javascript:

var simpleArray = <?=json_encode($simpleArray)?>;
//..lots of unrelated code
for(var i=0; i < IDs.length; i++)
{   
    console.log(simpleArray);
    //which color id is selected 
    var colorSelected = $j("#attribute92 option:selected").val();
    console.log('Value of color selected is ' + colorSelected);
    $j('.details'+data[i].vendor_id).append('<li class="priceBlock">$'+simpleArray[i][colorSelected]+'</li>');
}

Edit: I have gotten rid of the simpleArrayJson declaration in the php and changed the first line of the javascript.

share|improve this question
2  
Don't json_decode it, you want it in json format. I'm not too familar with php, but what is (array) for in your second snippet, first line? –  Kevin B May 23 '13 at 18:14
    
It was someone's suggestion in the PHP json_encode docs.. I have removed it. I now just have var simpleArray = <?=json_encode($simpleArray)?>; My issue now is accessing the value like it is an array: simpleArray[i][colorSelected]. All of the variables will be integers except $child->getPrice() which is a dollar value. –  CaitlinHavener May 23 '13 at 18:48
    
I don't understand why you are getting function Array() { [native code] } in your console.log. what browser are you testing with? –  Kevin B May 23 '13 at 18:51
    
chrome... hmmm... ill look in firefox. –  CaitlinHavener May 23 '13 at 19:01
    
Oh sorry, that was before I put the json_encode in there. It logs an object. Again, now I just need to be able to access that value simpleArray[i][colorSelected]. Those variables in there will be integers. –  CaitlinHavener May 23 '13 at 19:05

3 Answers 3

The is no reason for you to json_decode() the value you are trying to output. Just echo it directly:

var simpleArray = <?= $simpleArrayJson ?>;

This will output a javascript object literal.

share|improve this answer
    
var simpleArray = <?=json_encode($simpleArray)?>; with the rest of my code doesn't work –  CaitlinHavener May 23 '13 at 18:17
    
@CaitlinHavener That is because simpleArray is not a numerically indexed array. It is an object literal. you either need to modify the data structure you encode to JSON, or modify how you work with it in javascript. Take a look at the structure that is output to understand better how you can work with it. –  Mike Brant May 23 '13 at 18:20
    
That sounds like spanish. –  CaitlinHavener May 23 '13 at 18:21
    
@CaitlinHavener OK back to basics I guess. Do you know the difference between a numerically indexed array and an associative array in PHP? In javascript, do you know the difference between a numerically indexed array and an object (the closest thing to associative array in javascript)? –  Mike Brant May 23 '13 at 18:25
    
Do you understand that in your PHP, you appear to be building an associative array (unless $child->getVendor() returns integer values)? Further when you use JSON_FORCE_OBJECT flag with json_encode(), even if you have a numerically indexed array, that you would cause an object to be formed in your JSON serialization. You then try to access simpleArray in JS using numeric indexes, which there is no way you have created. You have a javascript object, you need to access it as such. Alternatively, you can build a true 0-based numerically indexed array in PHP and not use JSON_FORCE_OBJECT –  Mike Brant May 23 '13 at 18:29

Remove from the php.

$simpleArrayJson=json_encode($simpleArray, JSON_FORCE_OBJECT);

here you are converting the php array into a json string.

Change in the javascript

var simpleArray = <?= json_encode($simpleArray, JSON_FORCE_OBJECT); ?>;

Here you are just outputting the sting. previously you where doing this

var simpleArray = <?=(array) json_decode($simpleArrayJson)?>

which after json_decode was returning an array, which you where casting to an array which then was cast to a string by the <?= so what ended up going to your browser was something like:

var simpleArray = Array; 
share|improve this answer

try a for in loop.

for( item in data ) {
    console.log(data[item]);
}

this is because json has keys that match the indexes of the array that was json_encoded, instead of necessarily 0->n indexes.

Edit thanks to comments changed data.item to data[item]

share|improve this answer
    
for( item in simpleArray ) { console.log(simpleArray.item); } –  CaitlinHavener May 23 '13 at 18:59
    
yeilds undefined –  CaitlinHavener May 23 '13 at 19:00
1  
Shouldn't that be console.log(item) or console.log(data[item])? console.log(data.item) is wrong unless data has an item property. –  Kevin B May 23 '13 at 19:09

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.