简体   繁体   English

访问Json_encoded数组中的矩阵数组值

[英]Accessing Matrix Array Value in Json_encoded Array

I have a php array that I assigned to a javascript variable with json_encode. 我有一个用json_encode分配给javascript变量的php数组。 The php array is numerical not associative. php数组是数字而不是关联的。

Example: simpleArray[5][7]=1.50. 示例: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. 我需要能够根据索引值对数组进行json_encoded后访问1.50。

PHP: 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]

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). 根据您的for循环定义,您可以让i值为0、1、2,这些值在所示的对象中不存在(该属性的键为: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. 同样,您的for循环与您对象中的项目数量完全没有关系,我不确定这是故意的。

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: 另外, colorSelected通过调用val()来获得其值,该调用返回一个您可能想将其定义所在的行更改为的字符串:

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

This will make it an integer value. 这将使其成为整数值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM