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!

Link to comment
Share on other sites

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

        )

 

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.  :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.