如何在不使用键/索引值的情况下读取数组值? [英] How can I read array values without using key/index value?

查看:94
本文介绍了如何在不使用键/索引值的情况下读取数组值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



伙计,


这是我的问题:


我正在创建一个数组一个文件使用一些PHP和MySQL的reslut

处理。我的阵列的一个例子是


examlpe [''a''] =" example one" ;;

examlpe [''b''] =" example two"

examlpe [''c''] =" example three";

examlpe [''d''] =" example四个;

examlpe [''e''] =" example five";

examlpe [''f''] =" example six"" br />

数组键(或索引)可以有不同的值(虽然总是唯一的)。


我希望能够引用/读取,例如,数组中的第二个和第四个
元素 - 但由于我的数组键/索引是动态的,我的
标准脚本只会知道该数组被称为''例子''...


所以...使用上面的例子,我不知道

中的第一个元素数组是元素[' 'a'']。


我的问题:是否有一种方法可以用来引用

数组的任何部分,wi知道元素键吗?


如果没有,是否有方法将数组转换为数字键?


谢谢,

Randell D.

解决方案

>我正在使用一些PHP和MySQL

处理的reslut在文件中创建一个数组。我的数组的一个例子是

examlpe [''a''] =" example one" ;;
examlpe [''b''] =" example two""检查[''c''] ="示例三" ;;
检查[''d''] ="示例四" ;;
检查[''e''] = example five;
examlpe [''f''] =" example six"

数组键(或索引)可以有不同的值(尽管总是唯一的)。

我希望能够引用/读取数组中的第二个和第四个元素 - 但由于我的数组键/索引是动态的,我的
标准脚本只知道数组被称为''example''...

所以...使用上面的例子,我不知道数组中的第一个元素是元素[''a'']。

我的问题:是否有一种方法可以用来引用
数组的任何部分,而不知道元素键?
<如果没有,是否有合作的方法将数组转换为数字键?




买一本书,伙计。你正在处理几个误解。有些人

喜欢故意无知。我觉得你不是那些人之一。


在JavaScript中,一组命名值被称为对象。值是

无序。您可以使用for..in语句来检索值并忽略

奇数值,但订单不保证稳定。


JavaScript有一个数组类型,它采用整数下标。这可能是你想要的
。如果是这样,你在哪里得到了你必须使用

字母作为下标的坚定想法?

http://www.crockford.com/javascript/javascript.html


blockquote> Randell D.说:



伙计,

这是我的问题:
我正在使用一些PHP和MySQL
处理的重新创建在文件中创建一个数组。我的数组的一个例子是

examlpe [''a''] =" example one" ;;
examlpe [''b''] =" example two""检查[''c''] ="示例三" ;;
检查[''d''] ="示例四" ;;
检查[''e''] = example five;
examlpe [''f''] =" example six"

数组键(或索引)可以有不同的值(尽管总是唯一的)。

我希望能够引用/读取数组中的第二个和第四个元素 - 但由于我的数组键/索引是动态的,我的
标准脚本只知道数组被称为''example''...

所以...使用上面的例子,我不知道数组中的第一个元素是元素[''a'']。

我的问题:是否有一种方法可以用来引用
数组的任何部分,而不知道元素键?

如果没有,是否有转换数组的方法有数字键吗?




显而易见的解决办法似乎是改变你的PHP代码来创建它

就像你希望的那样能够访问它:


example [0] =" example one";

example [1] =" example two";

示例[2] ="示例三" ;;

示例[3] ="示例四" ;;

示例[4] ="示例五;

示例[5] ="示例六;


或:


示例[example.length] =" example one";

example [example.length] =" example two" ;;

example [example.length] ="示例三;

示例[example.length] ="示例四" ;;

示例[example.length] ="示例五" ;;

示例[example.length] ="示例六" ;;




" Douglas Crockford" <无**** @ covad.net>在消息中写道

news:27 ************************** @ msgid.meganewsse rvers.com ...

我正在使用一些PHP和MySQL
处理的重新创建文件中的数组。我的数组的一个例子是

examlpe [''a''] =" example one" ;;
examlpe [''b''] =" example two""检查[''c''] ="示例三" ;;
检查[''d''] ="示例四" ;;
检查[''e''] = example five;
examlpe [''f''] =" example six"

数组键(或索引)可以有不同的值(总是唯一的
虽然)。
我希望能够引用/读取数组中的第二个和第四个元素 - 但由于我的数组键/索引是动态的,我的
标准脚本只知道数组被称为''example''...

所以...使用上面的例子,我不知道数组中的第一个元素
是元素[''a'']。

我的问题:是否有一种方法可以用来引用数组的
的任何部分,而不知道元素键?
<如果没有,是否有一种方法可以召集rt阵列有数字键?
买书,伙计。你正在处理几个误解。一些



的人喜欢故意无知。我觉得你不是那些
人中的一个。
在JavaScript中,一组命名值被称为对象。
值是无序的。您可以使用for..in语句来检索值,并且
忽略奇数值,但是命令不保证稳定。

JavaScript有一个数组类型,它接受整数下标。这可能是你想要的
。如果是这样,你在哪里得到了你必须使用
字母作为下标的坚定想法?

http://www.crockford.com/javascript/javascript.html




我'我打破了 - 没有工作,没有多少积蓄可以带我前往工作

许可证获得批准......其次,我已经在我的圣诞节上放了一本javascript书

wish列表...我依赖于旧的javascript书籍和在线指南,我读了b $ b。我想我使用字母作为下标的坚定想法可能有来自PHP的
但是,虽然我不记得在哪里,我以为我见过一个

这样做的人(即用过的字母(而不是变量)来确定数组中的元素。


无论如何......我'' ll与for ... in循环一起工作,看看这对我有什么帮助......我不能用PHP预先分配数组的数字下标部分作为

预先创建几个javascript文件(包含数组)和web

页面包含所需数量或者少数几个...我想制作

确保没有重复写入预先存在的数组元素....


感谢您的帮助......



Folks,

Here''s my problem:

I am createing an array in a file using the reslut of some PHP and MySQL
processing. An example of my array would be

examlpe[''a'']="example one";
examlpe[''b'']="example two";
examlpe[''c'']="example three";
examlpe[''d'']="example four";
examlpe[''e'']="example five";
examlpe[''f'']="example six";

The array key (or index) can have different values (always unique though).

I want to be able to reference/read, for example, the second and fourth
element in the array - but since my array keys/indexes are dynamic, my
standard script will only know the array is called ''example''...

So... using the example above, I would not know that the first element in
the array is element[''a''].

My Question: Is there a method that I can use to reference any part of the
array, without knowing the elements key?

If not, is there a method to convert the array to have numeric keys?

Thanks,
Randell D.

解决方案

> I am createing an array in a file using the reslut of some PHP and MySQL

processing. An example of my array would be

examlpe[''a'']="example one";
examlpe[''b'']="example two";
examlpe[''c'']="example three";
examlpe[''d'']="example four";
examlpe[''e'']="example five";
examlpe[''f'']="example six";

The array key (or index) can have different values (always unique though).

I want to be able to reference/read, for example, the second and fourth
element in the array - but since my array keys/indexes are dynamic, my
standard script will only know the array is called ''example''...

So... using the example above, I would not know that the first element in
the array is element[''a''].

My Question: Is there a method that I can use to reference any part of the
array, without knowing the elements key?

If not, is there a method to convert the array to have numeric keys?



Buy a book, man. You are dealing with several misunderstandings. Some people
enjoy being willfully ignorant. I sense that you are not one of those people.

In JavaScript, a collection of named values is called an object. The values are
unordered. You can use a for..in statement to retrieve the values and ignore the
odd ones, but the order is not guarenteed to be stable.

JavaScript has an array type which takes integer subscripts. This is probably
what you want. If so, where did you get the nutty idea that you had to use
letters as subscripts?

http://www.crockford.com/javascript/javascript.html


Randell D. said:



Folks,

Here''s my problem:

I am createing an array in a file using the reslut of some PHP and MySQL
processing. An example of my array would be

examlpe[''a'']="example one";
examlpe[''b'']="example two";
examlpe[''c'']="example three";
examlpe[''d'']="example four";
examlpe[''e'']="example five";
examlpe[''f'']="example six";

The array key (or index) can have different values (always unique though).

I want to be able to reference/read, for example, the second and fourth
element in the array - but since my array keys/indexes are dynamic, my
standard script will only know the array is called ''example''...

So... using the example above, I would not know that the first element in
the array is element[''a''].

My Question: Is there a method that I can use to reference any part of the
array, without knowing the elements key?

If not, is there a method to convert the array to have numeric keys?



The obvious solution seems to be to change your PHP code to create it
the way that you want to be able to access it:

example[0]="example one";
example[1]="example two";
example[2]="example three";
example[3]="example four";
example[4]="example five";
example[5]="example six";

or:

example[example.length]="example one";
example[example.length]="example two";
example[example.length]="example three";
example[example.length]="example four";
example[example.length]="example five";
example[example.length]="example six";



"Douglas Crockford" <no****@covad.net> wrote in message
news:27**************************@msgid.meganewsse rvers.com...

I am createing an array in a file using the reslut of some PHP and MySQL
processing. An example of my array would be

examlpe[''a'']="example one";
examlpe[''b'']="example two";
examlpe[''c'']="example three";
examlpe[''d'']="example four";
examlpe[''e'']="example five";
examlpe[''f'']="example six";

The array key (or index) can have different values (always unique though).
I want to be able to reference/read, for example, the second and fourth
element in the array - but since my array keys/indexes are dynamic, my
standard script will only know the array is called ''example''...

So... using the example above, I would not know that the first element in the array is element[''a''].

My Question: Is there a method that I can use to reference any part of the array, without knowing the elements key?

If not, is there a method to convert the array to have numeric keys?
Buy a book, man. You are dealing with several misunderstandings. Some


people enjoy being willfully ignorant. I sense that you are not one of those people.
In JavaScript, a collection of named values is called an object. The values are unordered. You can use a for..in statement to retrieve the values and ignore the odd ones, but the order is not guarenteed to be stable.

JavaScript has an array type which takes integer subscripts. This is probably what you want. If so, where did you get the nutty idea that you had to use
letters as subscripts?

http://www.crockford.com/javascript/javascript.html



I''m broke - no work and little savings to carry me forward until a work
permit gets approved... Secondly, I have put a javascript book on my xmas
wish list... I''m reliant on old javascript books and online guides that I
read. I guess my nutty idea of using letters as subscripts might have
originated from PHP but, while I can''t recall where, I thought I''d seen an
examlpe of someone who had done such (ie used letters (and not variables) to
identify elements in an array.

Anyway... I''ll work with the for...in loop and see how that helps me... I
can''t have PHP pre-assign the numeric subscript portion of the array as
several javascript files (containing arrays) are created in advance and web
pages include as many, or as few of them as are needed... I want to make
sure there are no duplicates over-write preexisting array elements....

Thanks for the help though...


这篇关于如何在不使用键/索引值的情况下读取数组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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