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条)

报告相同问题?

悬赏问题

  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致