我可以像数组一样使用 stdClass 吗?

发布于 2024-08-17 06:43:57 字数 432 浏览 7 评论 0原文

是否可以使 stdClass 对象像通用索引数组一样工作?

IE $数组=数组 ( [0] => 120 [1] =>第382章 [2] =>第552章 [3] =>第595章 [4] => 616 )

会像

$a = array();
$array[] = 120;
$array[] = 382;

一样构造,但如果我用一个对象这样做,它只会覆盖自身:

$obj = new stdClass;
$obj->a = 120;
$obj->a = 382;

我知道我每次都可以更改“a”,

抱歉,如果这是一个愚蠢的问题,但由于某种原因它让我难住了!

感谢任何帮助:)

is it possible to make stdClass objects work like a generically indexed array?

i.e.
$array = Array
(
[0] => 120
[1] => 382
[2] => 552
[3] => 595
[4] => 616
)

would be constructed like

$a = array();
$array[] = 120;
$array[] = 382;

etc.

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!

Appreciate any help :)

Dan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

涫野音 2024-08-24 06:43:57

简而言之,不,因为您始终必须为您的属性命名。使用 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.

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?

EDIT: Check out this question for more info.

离不开的别离 2024-08-24 06:43:57

不,你不能。像这样使用方括号 ([]) 在 PHP 中称为“ArrayAccess”,并且未在 stdClass 对象上实现。

听起来您可能想要类似的东西

$foo = new stdClass();
$foo->items = array();
$foo->items[] = 'abc';
$foo->items[] = '123';
$foo->items[] = 'you and me';

您也可以尝试将数组转换为stdClass来看看会发生什么。

$foo = array();
$foo[] = 'abc';
$foo[] = '123';
$foo[] = 'you and me';
$foo = (object) $foo;
var_dump($foo);

No, you can't. Using the brackets ([]) like that is called "ArrayAccess" in PHP, and is not implemented on the stdClass object.

It sounds like you might want something like

$foo = new stdClass();
$foo->items = array();
$foo->items[] = 'abc';
$foo->items[] = '123';
$foo->items[] = 'you and me';

You could also try casting an array as a stdClass to see what happens.

$foo = array();
$foo[] = 'abc';
$foo[] = '123';
$foo[] = 'you and me';
$foo = (object) $foo;
var_dump($foo);
探春 2024-08-24 06:43:57

我真的不明白你想要做什么,除非

<?php
$obj->a = array();
$obj->a[] = 120;
$obj->a[] = 382;
// ...
?>

你不能简单地将一个字段“推”到一个对象上。无论如何,您都需要知道字段的名称才能检索值,所以这几乎是无稽之谈。

I can't really see what you mean to do, short of

<?php
$obj->a = array();
$obj->a[] = 120;
$obj->a[] = 382;
// ...
?>

You cannot simply "push" a field onto an object. You need to know the name of the field in order to retrieve the value anyways, so it's pretty much nonsense.

浅唱ヾ落雨殇 2024-08-24 06:43:57

对象不是数组。如果您想要数字索引元素,请使用数组。

如果您想要命名元素,请使用对象或关联数组。

至于为什么你会得到不同的行为,这是因为在数组中,你没有指定索引,所以 PHP 使用数组的长度作为索引。对于对象,您必须指定名称(在本例中为“a”)。

您可能想做的另一件事是拥有一个对象,其成员是数组:

$obj = new stdClass;
$obj->arr = array();
$obj->arr[] = 'foo';
$obj->arr[] = 'bar';

也不要忘记您可以将数组转换为对象:

$obj = (object) array(
    'arr' => array(
        'foo',
        'bar'
    )
);

An object is not an array. If you want numerically indexed elements, use an array.

If you want named elements use either an Object or an Associative Array.

As for why you're getting different behaviour, it's because in the Array, you are not specifying an index, so PHP uses the length of the Array as the index. With the Object, you have to specify the name (in this case 'a').

The other thing you may want to do is have an Object, with a member that is an array:

$obj = new stdClass;
$obj->arr = array();
$obj->arr[] = 'foo';
$obj->arr[] = 'bar';

also don't forget you can cast an array to an object:

$obj = (object) array(
    'arr' => array(
        'foo',
        'bar'
    )
);
宣告ˉ结束 2024-08-24 06:43:57

只是为了 OOP,不要使用类。如果您确实想实现此功能,请使用此类:

class ArrayClass
{
    protected $storage = array();

    public function push($content) {
        $this->storage[] = $content;
        return $this;
    }

    public function get($index) {
        if (isset($this->storage[$index])) {
            return $this->storage[$index];
        } else {
            //throw an error or
            return false;
        }
    }
}

Only for the sake of OOP, don't use classes. If you really want to implement this functionality, use this class:

class ArrayClass
{
    protected $storage = array();

    public function push($content) {
        $this->storage[] = $content;
        return $this;
    }

    public function get($index) {
        if (isset($this->storage[$index])) {
            return $this->storage[$index];
        } else {
            //throw an error or
            return false;
        }
    }
}
香橙ぽ 2024-08-24 06:43:57

您可以在 JavaScript 中准确地得到这一点。

You can get exactly this in JavaScript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文