我正在开展一个项目,我可以为project
分配一些状态,这些状态保存在表assigned_statuses
中。现在我使用jquery连接列表来决定用户如何分配它们。因此,当我加载两个列表时,将使用已分配的列表填充一个,而使用可用的列表填充另一个列表。
要获取指定的模型,我有以下模型函数:
应用程序/模型/ AssignedStatus.php
public function projects() {
return $this->belongsTo('Project');
}
public function statuses() {
return $this->belongsTo('Status');
}
应用程序/模型/ Project.php
public function assignedProjectBoardStatuses() {
return $this->hasMany('AssignedProjectBoardStatus','project_id')->orderBy('order','asc');
}
应用程序/模型/ Status.php
public function assignedProjectBoardStatus() {
return $this->hasMany('AssignedProjectBoardStatus');
}
我呼吁这些如下:
$project = Project::with(['assignedStatuses', 'assignedStatuses.statuses'])->find($id);
我抓住的状态如下:
$statuses = Status::statusByOrder();
现在返回一个数组,如下所示:
$project
数组:
array(12) {
["id"]=>
string(1) "3"
["name"]=>
string(11) "Project 13"
["start_date"]=>
string(10) "2015-07-13"
["end_date"]=>
string(10) "2015-07-27"
["active"]=>
string(1) "0"
["cancelled"]=>
string(1) "0"
["archive"]=>
string(1) "0"
["deleted_at"]=>
NULL
["created_at"]=>
string(19) "2015-07-26 00:01:59"
["updated_at"]=>
string(19) "2015-07-26 00:01:59"
["assigned_statuses"]=>
array(4) {
[0]=>
array(7) {
["id"]=>
string(1) "1"
["project_id"]=>
string(1) "3"
["status_id"]=>
string(1) "1"
["order"]=>
string(1) "0"
["created_at"]=>
string(19) "2015-07-26 00:01:59"
["updated_at"]=>
string(19) "2015-07-26 00:01:59"
["statuses"]=>
array(8) {
["id"]=>
string(1) "1"
["name"]=>
string(7) "Backlog"
["order"]=>
string(1) "0"
["created_at"]=>
string(19) "2015-07-25 23:22:54"
["updated_at"]=>
string(19) "2015-07-25 23:23:03"
}
}
[1]=>
array(7) {
["id"]=>
string(1) "2"
["project_id"]=>
string(1) "3"
["status_id"]=>
string(1) "3"
["order"]=>
string(1) "1"
["created_at"]=>
string(19) "2015-07-26 00:02:00"
["updated_at"]=>
string(19) "2015-07-26 00:02:00"
["statuses"]=>
array(8) {
["id"]=>
string(1) "3"
["name"]=>
string(11) "In Progress"
["order"]=>
string(1) "1"
["created_at"]=>
string(19) "2015-07-25 23:22:54"
["updated_at"]=>
string(19) "2015-07-25 23:23:04"
}
}
[2]=>
array(7) {
["id"]=>
string(1) "3"
["project_id"]=>
string(1) "3"
["status_id"]=>
string(1) "4"
["order"]=>
string(1) "2"
["created_at"]=>
string(19) "2015-07-26 00:02:00"
["updated_at"]=>
string(19) "2015-07-26 00:02:00"
["statuses"]=>
array(8) {
["id"]=>
string(1) "4"
["name"]=>
string(6) "Review"
["order"]=>
string(1) "2"
["created_at"]=>
string(19) "2015-07-25 23:22:54"
["updated_at"]=>
string(19) "2015-07-25 23:23:05"
}
}
[3]=>
array(7) {
["id"]=>
string(1) "4"
["project_id"]=>
string(1) "3"
["status_id"]=>
string(1) "5"
["order"]=>
string(1) "3"
["created_at"]=>
string(19) "2015-07-26 00:02:00"
["updated_at"]=>
string(19) "2015-07-26 00:02:00"
["statuses"]=>
array(8) {
["id"]=>
string(1) "5"
["name"]=>
string(4) "Done"
["order"]=>
string(1) "3"
["created_at"]=>
string(19) "2015-07-25 23:22:54"
["updated_at"]=>
string(19) "2015-07-25 23:23:08"
}
}
}
}
和statuses
数组:
array(5) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["name"]=>
string(7) "Backlog"
["order"]=>
string(1) "0"
["created_at"]=>
string(19) "2015-07-25 23:22:54"
["updated_at"]=>
string(19) "2015-07-25 23:23:03"
}
[1]=>
array(8) {
["id"]=>
string(1) "3"
["name"]=>
string(11) "In Progress"
["order"]=>
string(1) "1"
["created_at"]=>
string(19) "2015-07-25 23:22:54"
["updated_at"]=>
string(19) "2015-07-25 23:23:04"
}
[2]=>
array(8) {
["id"]=>
string(1) "4"
["name"]=>
string(6) "Review"
["order"]=>
string(1) "2"
["created_at"]=>
string(19) "2015-07-25 23:22:54"
["updated_at"]=>
string(19) "2015-07-25 23:23:05"
}
[3]=>
array(8) {
["id"]=>
string(1) "5"
["name"]=>
string(4) "Done"
["order"]=>
string(1) "3"
["created_at"]=>
string(19) "2015-07-25 23:22:54"
["updated_at"]=>
string(19) "2015-07-25 23:23:08"
}
[4]=>
array(8) {
["id"]=>
string(1) "2"
["name"]=>
string(4) "Open"
["order"]=>
string(1) "4"
["created_at"]=>
string(19) "2015-07-25 23:22:54"
["updated_at"]=>
string(19) "2015-07-25 23:23:01"
}
}
现在这就是我到目前为止:
if(array_key_exists('assigned_statuses', $project)) {
foreach ($project->assigned_statuses as $status) {
$assignedIds [] = $status->id;
}
foreach ($statuses as $status) {
$statusIds[] = $status->id;
}
if (in_array($assignedIds, $statusIds)) {
unset($statusIds);
} else {
//pass all available statuses...
}
}
然而,这并没有达到未设置的部分,因为两个阵列的比较失败了。有没有人知道如何比较两个数组之间的ID?在$ project数组中,我想针对$ status ids检查嵌套数组[" assigned_statuses"]。如何成功实现这一目标并取消已经分配的那些?
答案 0 :(得分:0)
如果$assigned_statuses
和$statuses
都是有效数组,您可以使用array_intersect函数比较它们,如下所示:
$variable = array_intersect($assigned_statuses, $statuses);
这将在值中搜索相似性,并将在两个数组之间的所有匹配值的$variable
变量中构造另一个数组。
然后,您可以使用array_diff方法在两组数组中查找唯一值