我有一系列问题分配和一系列问题。我想通过迭代分配的问题并从问题数组中删除这些问题的ID来从问题数组中删除指定的那些。
这应该给我留下一系列未分配的问题。
例如
assigned_questions(1,2,3,4,5)
问题(1,2,3,4,5,6,7,8,9,10)
应该离开
问题(6,7,8,9,10)
所以我的赋值数组包含一个survey_questions_id,所以我正在做一个foreach并在问题数组中取消设置问题的id,但是我没有得到一个数组,其中包含被删除的问题,
public function remove_assigned_questions($questions, $survey_id) {
$assignments = $this->db->rs( T_SURVEY_QUESTION_ASSIGNMENTS, NULL, (T_SURVEYS . TC_ID_SUFFIX .'=' . $survey_id) );
foreach($assignments as $assignment) {
unset($questions[$assignment[T_SURVEY_QUESTIONS . TC_ID_SUFFIX]]);
}
return $questions;
}
我的问题数组看起来像这样
array(3) {
[0]=>
array(5) {
["id"]=>
string(1) "1"
["survey_question"]=>
string(13) "Some question"
["survey_question_types_id"]=>
string(1) "1"
["required"]=>
string(1) "1"
["status"]=>
string(1) "1"
}
[1]=>
array(5) {
["id"]=>
string(1) "2"
["survey_question"]=>
string(17) "One more question"
["survey_question_types_id"]=>
string(1) "2"
["required"]=>
string(1) "1"
["status"]=>
string(1) "1"
}
[2]=>
array(5) {
["id"]=>
string(1) "3"
["survey_question"]=>
string(21) "Just another question"
["survey_question_types_id"]=>
string(1) "1"
["required"]=>
string(1) "1"
["status"]=>
string(1) "1"
}
}
任何想法为什么它只是没有删除赋值[T_SURVEY_QUESTIONS。 TC_ID_SUFFIX]
以下是赋值的值,以及赋值的foreach循环值[T_SURVEY_QUESTIONS。 TC_ID_SUFFIX]
array(2) {
[0]=>
array(3) {
["id"]=>
string(1) "1"
["surveys_id"]=>
string(1) "3"
["survey_questions_id"]=>
string(1) "1"
}
[1]=>
array(3) {
["id"]=>
string(1) "2"
["surveys_id"]=>
string(1) "3"
["survey_questions_id"]=>
string(1) "2"
}
}
string(1) "1"
string(1) "2"
当程序运行时,它似乎删除了ids 2和3的问题并留下了1
array(1) {
[0]=>
array(5) {
["id"]=>
string(1) "1"
["survey_question"]=>
string(13) "Some question"
["survey_question_types_id"]=>
string(1) "1"
["required"]=>
string(1) "1"
["status"]=>
string(1) "1"
}
}
这是不正确的,因为分配了
答案 0 :(得分:0)
我怀疑感谢@deceze指出我做错了什么,我需要在问题中找到问题数组的关键字,其中包含已分配的问题值,然后删除该数组键
public function remove_assigned_questions($questions, $survey_id) {
$assignments = $this->db->rs( T_SURVEY_QUESTION_ASSIGNMENTS, NULL, (T_SURVEYS . TC_ID_SUFFIX . '=' . $survey_id) );
$ids = array_column($questions, TC_ID);
foreach($assignments as $assignment) {
if( $key = array_search( $assignment[T_SURVEY_QUESTIONS . TC_ID_SUFFIX], $ids ) );
unset($questions[$key]);
}
return $questions;
}
排序;)