简体   繁体   English

访问数组中对象的值

[英]Accessing the value of an object in an array

Consider the following array of objects: 考虑以下对象数组:

class Person {
    public $name;
    public $occupation;
    public function __construct($n, $o){
        $this->name = $n;
        $this->occupation = $o;
    }
}

$people = array(
    new Person("John",   "singer"),
    new Person("Paul",   "guitar"),
    new Person("George", "bass"),
    new Person("Ringo",  "drums")
);

Is there any quick way to access the objects? 有没有快速访问对象的方法? I wouldn't mind storing them in a different datatype (as opposed to array) if another datatype could make access easier. 如果另一种数据类型可以使访问更容易,我不介意将它们存储在不同的数据类型(而不是数组)中。

Example of accessing an object: I would like to now change the "Paul" object to have an occupation of singer. 访问对象的示例:我现在想要更改“Paul”对象以占用歌手。 This is the current solution: 这是目前的解决方案:

foreach ( $people as &$p ) {
        if ( $p->name=="Paul" )
                $p->occupation="singer";
}

Alternatively, I might need to access based on a different property: Let's change all the singers' names to Yoko: 或者,我可能需要根据不同的属性访问:让我们将所有歌手的名字改为Yoko:

foreach ( $people as &$p ) {
        if ( $p->occupation=="singer" )
                $p->="Yoko";
}

Another example of accessing an object, this time in order to get the occupation of Ringo: 另一个访问对象的例子,这次是为了占领Ringo:

$ringosOccupation="";
foreach ( $people as $p ) {
        if ( $p->name=="Ringo" )
                $ringosOccupation = $p->occupation;
}

I suppose that I could write a People class that stores each Person object in an internal array and supplies functions to change or read occupation, but if PHP has anything cleverer build in I would love to know. 我想我可以编写一个People类,它将每个Person对象存储在一个内部数组中,并提供更改或读取占用的函数,但如果PHP有更聪明的构建,我很想知道。

Thanks. 谢谢。

Why aren't you just setting the key of the elements to the name? 为什么不只是将元素的键设置为名称?

$people = array(
    'john'   => new Person("John",   "singer"),
    'paul'   => new Person("Paul",   "guitar"),
    'george' => new Person("George", "bass"),
    'ringo'  => new Person("Ringo",  "drums"),
);

Just index your array with the names: 只需使用名称索引数组:

$people = array(
    "John" => new Person("John",   "singer"),
    "Paul" => new Person("Paul",   "guitar"),
    "George" => new Person("George", "bass"),
    "Ringo" => new Person("Ringo",  "drums")
);

// Paul is the new drummer:
$people["Paul"]->occupation = "drums";

It creates a little bit of redundancy, but surely that redundancy won't be more memory or compute intensive than looping over all of them to locate the one you need every time you need to modify something. 它创建了一点冗余,但肯定的是,冗余不会比在所有需要修改内容的情况下循环遍历所有内存或计算密集型更多。

Update: 更新:

After the question was updated, it is clear that names may be non-unique or other properties needed for access. 问题更新后,很明显名称可能是非唯一的或访问所需的其他属性。 In that case, you might be better off using a database to store object state if you have to do it often. 在这种情况下,如果您不得不经常使用数据库来存储对象状态,那么最好。 You can't escape needing to iterate over the array if it can't be uniquely indexed. 如果不能对数组进行唯一索引,则无法逃避需要迭代数组。 It is trivially easy to make these changes in a database, but you would need to be rebuilding the objects all the time. 在数据库中进行这些更改非常容易,但您需要始终重建对象。

So, if your array is not too large, keep looping like you have been. 因此,如果您的数组不是太大,请像往常一样保持循环。 If it meets your performance needs, its an ok method. 如果它满足您的性能需求,那就是一个好的方法。 If you have lots and lots of these to modify, and modify often, I would suggest storing them in a database and building the objects only when you need to read one out. 如果你有很多很多这些要修改和修改,我建议将它们存储在一个数据库中,只有当你需要读出一个对象时才能构建它们。 Then you could do: 然后你可以这样做:

UPDATE people SET name = 'Yoko' WHERE occupation = 'singer'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM