Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8439119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:55:11+00:00 2026-06-10T07:55:11+00:00

Consider the following array of objects: class Person { public $name; public $occupation; public

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:55:13+00:00Added an answer on June 10, 2026 at 7:55 am

    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'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following 2 programs giving same error First calss: public class Testing { Testing
Consider the following proxy class: class VertexProxy { public: VertexProxy(double* x, double* y, double*
Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[]
please consider the following: I have a queue of objects represented as an array.
Consider the following code: class myarray { int i; public: myarray(int a) : i(a){
Consider following class class test { public: test(int x){ cout<< test \n; } };
consider the following code: class MyClass(object): def __init__(self): self.data_a = np.array(range(100)) self.data_b = np.array(range(100,200))
Please consider the following code: <?php class MyException extends Exception {} function global_exception_handler($exception) {
Consider following class of custom array template <typename element, unsigned int size> class array
Consider the following associative array $arr = Array ( [banana] => 2 [cherry] =>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.