我可以使用stdClass的像一个数组? [英] can i use stdClass like an array?
问题描述
是有可能使stdClass的对象,像一个一般索引数组?
is it possible to make stdClass objects work like a generically indexed array?
即。
$阵列=阵列
(
[0] => 120
[1] => 382
[2] => 552
[3] => 595
[4] => 616
)
i.e. $array = Array ( [0] => 120 [1] => 382 [2] => 552 [3] => 595 [4] => 616 )
将被修建像
$a = array();
$array[] = 120;
$array[] = 382;
等。
但如果我这样做有一个对象,它只是覆盖本身:
but if i do that with an object it just overwrites itself:
$obj = new stdClass;
$obj->a = 120;
$obj->a = 382;
我知道我可以改变'一'每一次,
i know i can change 'a' every time,
对不起,如果这是一个愚蠢的问题,但它的两腿发僵我出于某种原因!
sorry if this is a stupid question but it's stumping me for some reason!
鸭preciate任何帮助:)
Appreciate any help :)
丹
推荐答案
在短,没有,因为你总是要命名的属性。通过编写一个简单的类了ArrayAccess的麻烦去是没有意义不管是你已经基本上重新一无所有一个数组来显示它除了在性能和透明度极端的牺牲。
In short, no, because you will always have to name your properties. Going through the trouble of writing a simple class with ArrayAccess makes no sense either as you've essentially recreated an array with nothing to show for it except extreme sacrifices in performance and transparency.
有一些原因,你的问题不能无限更简单地用一个数组解决的,或者这只是一个愿望-I-知道的问题?
Is there some reason your problem cannot be solved infinitely more simply by using an array, or is this just a wish-I-knew question?
编辑:检查出<一个href=\"http://stackoverflow.com/questions/1869812/casing-an-array-with-numeric-keys-as-an-object\">this问题获取更多信息。
Check out this question for more info.
这篇关于我可以使用stdClass的像一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!