Jump to content

Removing elements of an array if there's something in common


kreut

Recommended Posts

Hello,

 

I have two arrays:

$questions, which has 'question_id' as one of its keys and

$assignment_questions which is a subset of $questions, pulled off my database.

 

Using php, I'd like to construct a 3rd array which takes my $questions array and removes from it any question that has a question_id which can be found in both $questions and $assignment_questions, in addition to the other values associated with a particular question_id.

 

 

For example, $questions has: question_id, solution, author (and other keys).  Is there any way to check if a particular key matches and then zap out all of the corresponding fields (solution, author, etc.)?

 

Thanks!

Sure!  The first is from my $questions array will the second is from my $assignments_questions array:

 

[8] => Array

        (

            [question_id] => 11

            [author_id] => 3

            [section_id] => 0

            [text] =>      What do you think of  pizza?

            [solution] => I love it

            [incorrect_solution1] =>

            [incorrect_solution2] =>

            [incorrect_solution3] =>

            [solution_order] =>

            [filename] => no_pic.png

            [question_type] => Free Response

            [public_access] => Yes

        )

 

)

Array

(

    [0] => Array

        (

            [question_id] => 2

            [author_id] => 1

            [section_id] => 0

            [text] =>    What's 3 +5?

            [solution] => 8

            [incorrect_solution1] => 1

            [incorrect_solution2] => 2

            [incorrect_solution3] => 5

            [solution_order] => 0231

            [filename] => no_pic.png

            [question_type] => Multiple Choice

            [public_access] => No

            [assignment_id] => 1

        )

 

Okay, so make a new multi-dim array ($result) with all $questions that do not appear in $assignment_questions:

 

foreach ($assignment_questions as $aq) {
  $aq_ids[] = $aq['question_id'];
}

foreach ($questions as $q) {
  if (!in_array($q['question_id'],$aq_ids)) {
    $result[] = $q;
  }
}	 

 

Random question though...are these arrays a result of a db query? I'm thinking it would probably be easier/better for you to grab this from a db query (as a new query or adding to existing query)

Yes, I fear that they are from a database query.  My original thought (and cleaner?) was to try this:

 

SELECT *
		FROM questions
		LEFT JOIN assignments_questions
		USING (question_id)
		WHERE ((questions.author_id = $user_id)
		AND (assignments_questions.question_id = NULL))

 

But....for the life of my I can't figure out why it's not working (I get nothing out!).  IF you happen to see the error of my ways, a database solution would definitely be preferable. However, the code the you sent me is helpful as well.  :)

well I'm not a sql expert but I think maybe something like this should work...(you might wanna ask in the sql forum)

 

SELECT * FROM questions WHERE question_id NOT IN (SELECT question_id FROM assignments_questions WHERE author_id = '$user_id')

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.