【问题标题】:PHP Laravel Check two arrays and unset valuesPHP Laravel 检查两个数组和未设置的值
【发布时间】:2015-10-16 10:36:09
【问题描述】:

嗨,我正在做一个项目,我可以将一些状态分配给 project,这些状态保存在表 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();

现在返回一个数组如下:

$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"]。我怎样才能成功地实现这一点并取消设置已经分配的那些??

【问题讨论】:

  • $sprint 定义或值在哪里?

标签: php arrays laravel laravel-4 unset


【解决方案1】:

如果$assigned_statuses 和$statuses 都是有效的数组,您可以像这样使用array_intersect 函数来比较它们:

$variable = array_intersect($assigned_statuses, $statuses); 这将搜索值的相似性,并将在两个数组之间所有匹配值的$variable 变量中构造另一个数组。

然后您可以使用 array_diff 方法在两组数组中查找唯一值

【讨论】:

  • 很好的答案我能够做到这一点
猜你喜欢
  • 1970-01-01
  • 2014-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-23
相关资源
最近更新 更多