George K
asked on
PHP get array item with custom id
I have the following code:
class Member {
public $id;
public $name;
public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}
}
$members[] = new Member(100, 'George');
$members[] = new Member(105, 'John');
And what I actually want to do is to find display the name of the member with ID 105 something like $members[105]->name
What is the proper way to do it?
class Member {
public $id;
public $name;
public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}
}
$members[] = new Member(100, 'George');
$members[] = new Member(105, 'John');
And what I actually want to do is to find display the name of the member with ID 105 something like $members[105]->name
What is the proper way to do it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The concept illustrated in the question is an array of objects. This looks like a classroom assignment, which is good, because learning about how to use an array of objects correctly is a really valuable lesson. It's one of the most useful data structures in PHP, or any other language.
For the test case, array keys are not really needed at all. Where there are only two data points and both of the data elements are specified in the exercise, there cannot be a collision on the array key. Given this specific data set, you can get away with changing the exercise to use the member id as an array key. This is typical of the problem with small test cases - they do not illustrate enough of the problem to be useful in the real world. The risk of using specific array keys is that there may someday be a third member with a key value that matches one of the other members, and when that happens the dataset will be damaged without notice (PHP does not bark about overwriting array elements). Something like this would cause the test to fail.
The nature of duplicate array keys can make sense when you want to "de-duplicate" a data set based on values in a specific field. Generally speaking, any integer or string can work well as an array key, but array keys may be subject to the rules of loose data typing.
More information about how to use PHP arrays:
http://php.net/manual/en/language.types.array.php
http://php.net/manual/en/function.array.php
http://php.net/manual/en/control-structures.foreach.php
General discussion on Variables and References:
https://www.experts-exchange.com/articles/12310/PHP-Variables-and-References.html
For the test case, array keys are not really needed at all. Where there are only two data points and both of the data elements are specified in the exercise, there cannot be a collision on the array key. Given this specific data set, you can get away with changing the exercise to use the member id as an array key. This is typical of the problem with small test cases - they do not illustrate enough of the problem to be useful in the real world. The risk of using specific array keys is that there may someday be a third member with a key value that matches one of the other members, and when that happens the dataset will be damaged without notice (PHP does not bark about overwriting array elements). Something like this would cause the test to fail.
$members[100] = new Member(100, 'George');
$members[105] = new Member(105, 'John');
$members[105] = new Member(106, 'Ray');
echo $members[105]->name;
As you can see from the code snippet, the Member object will get created, but when "Ray" is inserted into the array, the "John" member will be lost because array position #105 will be overwritten. The algorithm that uses the object->id will never find $member->id == 105. The algorithm that uses direct addressing to $member[105] will return the wrong data.The nature of duplicate array keys can make sense when you want to "de-duplicate" a data set based on values in a specific field. Generally speaking, any integer or string can work well as an array key, but array keys may be subject to the rules of loose data typing.
More information about how to use PHP arrays:
http://php.net/manual/en/language.types.array.php
http://php.net/manual/en/function.array.php
http://php.net/manual/en/control-structures.foreach.php
General discussion on Variables and References:
https://www.experts-exchange.com/articles/12310/PHP-Variables-and-References.html
ASKER
That is really smart. As long as the ID is always unique and cannot be overwrite by someone else, its an easy and quick solution to my problem :)
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
In the example, $members is an array. You would iterate over the array until you find the member(s) with id=105.
https://iconoun.com/demo/temp_georgek.php
Open in new window
OutputsOpen in new window