doushua7737 2013-05-23 20:18
浏览 59
已采纳

访问Json_encoded数组中的Matrix数组值

I have a php array that I assigned to a javascript variable with json_encode. The php array is numerical not associative.

Example: simpleArray[5][7]=1.50. I need to be able to access the 1.50 after the array has been json_encoded based on the index values.

PHP:

$simpleArray= [];   

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);//see the picture of me below
    var colorSelected = $j("#attribute92 option:selected").val(); //integer value

    $j('.details'+data[i].vendor_id).append('<li class="priceBlock">$'+simpleArray[i][colorSelected]+'</li>');
}

Console.log(simpleArray):

enter image description here

  • 写回答

1条回答 默认 最新

  • douyingbei1458 2013-05-23 20:29
    关注

    Here you are likely trying to access values that don't exist in your object:

    simpleArray[i][colorSelected]
    

    Based on your for loop definition, you can have i values of 0, 1, 2 which don't exist in the object shown (which has properties at keys: 3,4,5). Also your for loop has no relation at all to the number of items in your object, which I am not sure is intended.

    Also, colorSelected derives it's value from a call to val() which returns a string you you probably want to change the line where it is defined to:

    var colorSelected = parseInt($j("#attribute92 option:selected").val());
    

    This will make it an integer value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗