PHP在对象数组中找到匹配的属性值 [英] PHP find matching property values in an array of objects
问题描述
我有一个自定义对象数组,我想找到具有匹配属性值的对象.
I have an array of custom objects and I want to find the objects who have matching property values.
这是我的object.php:
This is my object.php:
class game_list
{
var $team_name;
var $field_nr;
var $time;
/**
* @return mixed
*/
public function getTeamName()
{
return $this->team_name;
}
/**
* @param mixed $team_name
*/
public function setTeamName($team_name)
{
$this->team_name = $team_name;
}
/**
* @return mixed
*/
public function getFieldNr()
{
return $this->field_nr;
}
/**
* @param mixed $field_nr
*/
public function setFieldNr($field_nr)
{
$this->field_nr = $field_nr;
}
/**
* @return mixed
*/
public function getTime()
{
return $this->time;
}
/**
* @param mixed $time
*/
public function setTime($time)
{
$this->time = $time;
}
}
所以我有一个数组,其中包含x个这些对象.我想找到具有相同 $ field_nr
和 $ time
值的对象.例如:
So I have an array with x number of these objects. I want to find objects which have the same $field_nr
and $time
values. For example:
如果我的数组中的两个对象X和Y都具有 $ field_nr = 1
和 $ time ="12:00"
,我想打印出匹配!".
If two objects on my array, X and Y, both have $field_nr = 1
and $time = "12:00"
I want to print out "Match!".
这是我目前正在做的事情
This is what I am doing currently:
//getPlaySchedule returns my array($feedback) of objects
$feedback= getPlaySchedule($cup_name, $cup_year, $division);
for($x=0; $x<count($feedback); $x++){
$time = $feedback[$x]->getTime();
$field = $feedback[$x]->getFieldNr();
$team = $feedback[$x]->getTeamName();
for($y=0; $y<count($feedback); $y++){
if($time == $feedback[$y]->getTime() && $field == $feedback[$y]->getFieldNr() && $team != $feedback[$y]->getTeamName()){
echo 'Match!';
}
}
}
但是,我的解决方案会打印出匹配!"每次比赛两次.有没有更好的方法可以在我的对象数组中找到这些匹配项?
My solution, however, prints out "Match!" two times for each match. Is there a better way of finding these matches in my object array?
马库斯
推荐答案
这是因为您没有存储,检查的内容以及每次循环遍历整个数组的原因.例如,如果您有对象A,B,C,D,E,F,则首先检查A.假设A将匹配D.当您选择D时,D将匹配A.
This is because you not store, what you've checked, and every time loop through the whole array. For example, if you have object A,B,C,D,E,F, first check the A. Let say, A will match D. When you check D, D will match A.
因此,您需要将其存储到数组中,或插入一个指针,检查已经检查了哪些对象,并且在检入D时,跳过A,B,C.
So you need to store it into an array, or insert a pointer, what objects checked already, and when checkin D, skip A, B, C.
一个可能的解决方案,如果您设置一个计数器(在我的示例中为 $ z
),然后从中开始第二个循环.这样可以防止脚本再次检查您已检查的内容.代码未经测试.
A possible solution, if you set a counter, in my example $z
, and start the second loop from that. This prevent the script to check again what you've already checked. Code not tested.
$z = 0;
for ($x = 0; $x < count($my_array); $x++) {
$time = $feedback[$x]->getTime();
$field = $feedback[$x]->getFieldNr();
$team = $feedback[$x]->getTeamName();
for ($y = $z; $y < count($my_array); $y++) {
if ($time == $feedback[$y]->getTime() && $field == $feedback[$y]->getFieldNr() && $team != $feedback[$y]->getTeamName()) {
echo 'Match!';
}
}
$z++;
}
这篇关于PHP在对象数组中找到匹配的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!