创建2D关联数组javascript(与php assoc数组相同) [英] Creating a 2d associative array javascript (same as a php assoc array)

查看:109
本文介绍了创建2D关联数组javascript(与php assoc数组相同)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用javascript创建一个数组,这将允许我访问像这样的数据:

I am trying to create an array in javascript which will allow me to access data like this:

var name = infArray[0]['name'];

但是,我似乎无法以这种方式进行任何工作.当我使用json_encode从php传递到javascript的assoc数组时,它以这种方式构造了数据. 我这样做的原因是,这样我就可以将相同格式的数据传回php,以执行更新sql请求.

however I cant seem to get anything to work in this way. When i passed out a assoc array from php to javascript using json_encode it structured the data in this way. The reason why i have done this is so i can pass back the data in the same format to php to execute an update sql request.

推荐答案

JavaScript没有关联数组.它具有(数字)数组和对象.

JavaScript doesn't have associative arrays. It has (numeric) arrays and objects.

您想要的是两者的结合.像这样:

What you want is a mix of both. Something like this:

var infArray = [{
    name: 'Test',
    hash: 'abc'
}, {
    name: 'something',
    hash: 'xyz'
}];

然后您可以按照显示的方式访问它:

Then you can access it like you show:

var name = infArray[0]['name']; // 'test'

或使用点符号:

var name = infArray[0].name; // 'test'

这篇关于创建2D关联数组javascript(与php assoc数组相同)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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