dongtou5557 2012-08-26 18:26
浏览 41
已采纳

访问数组中对象的值

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. 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:

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:

$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.

Thanks.

  • 写回答

2条回答 默认 最新

  • dtyqeoc70733 2012-08-26 18:29
    关注

    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'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?