PHP Laravel 检查两个数组并取消设置值
PHP Laravel Check two arrays and unset values
嗨,我正在做一个项目,我可以在其中为 project
分配一些状态,这些状态保存在 table assigned_statuses
中。现在我正在使用 jquery 连接列表来决定用户如何分配它们。因此,当我加载这两个列表时,一个将填充分配的列表,另一个将填充可用的列表。
为了获取分配的那些,我有以下模型函数:
app/models/AssignedStatus.php
public function projects() {
return $this->belongsTo('Project');
}
public function statuses() {
return $this->belongsTo('Status');
}
app/models/Project.php
public function assignedProjectBoardStatuses() {
return $this->hasMany('AssignedProjectBoardStatus','project_id')->orderBy('order','asc');
}
app/models/Status.php
public function assignedProjectBoardStatus() {
return $this->hasMany('AssignedProjectBoardStatus');
}
我呼吁如下:
$project = Project::with(['assignedStatuses', 'assignedStatuses.statuses'])->find($id);
以及我抓取的状态如下:
$statuses = Status::statusByOrder();
现在这个returns数组如下:
$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 数组中,我想根据 $statuses id 检查嵌套数组 ["assigned_statuses"]。我怎样才能成功实现这一目标并取消设置已经分配的那些?
如果 $assigned_statuses
和 $statuses
都是有效数组,您可以使用 array_intersect 函数比较它们,如下所示:
$variable = array_intersect($assigned_statuses, $statuses);
这将去搜索值的相似性,并将在两个数组之间的所有匹配值的 $variable
变量中构造另一个数组。
然后您可以使用 array_diff 方法在两组数组中查找唯一值
嗨,我正在做一个项目,我可以在其中为 project
分配一些状态,这些状态保存在 table assigned_statuses
中。现在我正在使用 jquery 连接列表来决定用户如何分配它们。因此,当我加载这两个列表时,一个将填充分配的列表,另一个将填充可用的列表。
为了获取分配的那些,我有以下模型函数:
app/models/AssignedStatus.php
public function projects() {
return $this->belongsTo('Project');
}
public function statuses() {
return $this->belongsTo('Status');
}
app/models/Project.php
public function assignedProjectBoardStatuses() {
return $this->hasMany('AssignedProjectBoardStatus','project_id')->orderBy('order','asc');
}
app/models/Status.php
public function assignedProjectBoardStatus() {
return $this->hasMany('AssignedProjectBoardStatus');
}
我呼吁如下:
$project = Project::with(['assignedStatuses', 'assignedStatuses.statuses'])->find($id);
以及我抓取的状态如下:
$statuses = Status::statusByOrder();
现在这个returns数组如下:
$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 数组中,我想根据 $statuses id 检查嵌套数组 ["assigned_statuses"]。我怎样才能成功实现这一目标并取消设置已经分配的那些?
如果 $assigned_statuses
和 $statuses
都是有效数组,您可以使用 array_intersect 函数比较它们,如下所示:
$variable = array_intersect($assigned_statuses, $statuses);
这将去搜索值的相似性,并将在两个数组之间的所有匹配值的 $variable
变量中构造另一个数组。
然后您可以使用 array_diff 方法在两组数组中查找唯一值