Search code examples
phparrayslaravellaravel-4unset

PHP Laravel Check two arrays and unset values


Hi im working on a project where I can assign some statuses to a project and these are saved in a table assigned_statuses. Now I am using jquery connected lists to decide how users assign them. Therefore when I load the two lists one will be populated with the assigned ones and the other with the available ones. To fetch the assigned ones I have the following Model functions:

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');
}

and I call upon these as follows:

$project = Project::with(['assignedStatuses', 'assignedStatuses.statuses'])->find($id);

and the statuses I grab as follows:

$statuses = Status::statusByOrder();

Now this returns an array as follows:

$project array:

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"
}
}
}
}

and the statuses array:

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"
}
}

Now this is what I have thus far:

    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...
        }
    }

However this doesn't reach the unset piece, I goes because the comparison of the two arrays fail. Does any know how I can compare the ids between the two arrays? In the $project array I want to check the nested array ["assigned_statuses"] against the $statuses ids. How can I achieve this successfully and unset the ones that are already assigned??


Solution

  • Well if $assigned_statuses and $statuses are both valid arrays, you could compare them using the array_intersect function like this:

    $variable = array_intersect($assigned_statuses, $statuses); This would go and search for similarities in values, and will construct another array in the $variable variable of all the matching values between both arrays.

    You could then use the array_diff method to find unique values within two sets of arrays