访问Json_encoded数组中的矩阵数组值 [英] Accessing Matrix Array Value in Json_encoded Array

查看:102
本文介绍了访问Json_encoded数组中的矩阵数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php数组,我用json_encode分配给了一个javascript变量. php数组是数字而不是关联的.

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

示例:simpleArray [5] [7] = 1.50.根据索引值对数组进行json_encoded后,我需要能够访问1.50.

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:

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):

Console.log(simpleArray):

推荐答案

在这里,您可能试图访问对象中不存在的值:

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

simpleArray[i][colorSelected]

根据您的for循环定义,您可以将i值分别设置为0、1、2,这些值在所示的对象中不存在(该属性的键为3、4、5).同样,您的for循环与您对象中的项目数量完全没有关系,我不确定这是故意的.

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.

此外,colorSelected从对val()的调用中获取其值,该调用将返回您可能要将其定义所在的行更改为的字符串:

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());

这将使其成为整数值.

这篇关于访问Json_encoded数组中的矩阵数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆