如何在java脚本中创建多维数组?

时间:2017-02-07 05:55:40

标签: javascript php jquery

我需要使用ajax将多个值传递给另一个页面,所以请帮助创建多维。

我需要这样的数组。

<script type="text/javascript">
    var concertArray = [
             ["Billy Joel", "99", "equal.png"],
             ["Bryan Adams", "89", "higher.png"],
             ["Brian Adams", "25", "lower.png"]
             ]; 
</script>

4 个答案:

答案 0 :(得分:0)

var concertArray = [ ["Billy Joel", "99", "equal.png"], ["Bryan Adams", "89", "higher.png"], ["Brian Adams", "25", "lower.png"] ];

这样可行。

您可以使用concertArray[i][j]

访问值

答案 1 :(得分:0)

请尝试以下代码。当然它会起作用。

var items = [
["Billy Joel", "99", "equal.png"],
  ["Bryan Adams", "89", "higher.png"],
  ["Brian Adams", "25", "lower.png"]
];
console.log(items[0][0]); // to access 1st array
console.log(items);

答案 2 :(得分:0)

你可以传递数组,但给JSON一个想法:

显示可能派上用场功能的小部件:

&#13;
&#13;
var concertArray = [
  ["Billy Joel", "99", "equal.png"],
  ["Bryan Adams", "89", "higher.png"],
  ["Brian Adams", "25", "lower.png"]
];

function arrayToJSON(arr, keys) {
  var obj = {};
  arr.forEach(function(value, index) {
    var tempObj = {};
    value.forEach(function(innerValue, innerindex) {
      tempObj[keys[innerindex]] = innerValue;
    })
    obj[index] = tempObj;
  });
  return obj;
};

console.log(arrayToJSON(concertArray, ['name', 'age', 'avatar']))
&#13;
&#13;
&#13;

答案 3 :(得分:0)

var m;
var click_qty = [];
for(l=1;l<cont;l++)
 {
   var tst1=document.getElementById('tst1'+m).value;
   var tst2=document.getElementById('tst2'+m).value;
   n = l -1;
   click_qty[n]=[tst1,tst2];
 }

$.ajax({
       type: 'POST',
       url:'ajax_result.php',
       data:{           click_qty:JSON.stringify(click_qty), },
       success: function(msg1){
          //your code
       }

解码这个json使用像thiS

$new_val = str_replace('],[','!~',$_POST['click_qty']);     
$new_val = str_replace('\"',"",$new_val);   
$new_val = str_replace('[[',"",$new_val);   
$new_val = str_replace(']]',"",$new_val);   
$fnl_prod = explode("!~",$new_val);     
$fnl_count = count($fnl_prod);  
$new_display = "";  
$fnd_val = "";  

for($i=0;$i<$fnl_count;$i++)    
{       
   $res_prod = explode(',',$fnl_prod[$i]);
   $res_prod[0] //------>tst1
   $res_prod[1] //------>tst2
}