kreut Posted February 21, 2011 Share Posted February 21, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/228440-removing-elements-of-an-array-if-theres-something-in-common/ Share on other sites More sharing options...
.josh Posted February 21, 2011 Share Posted February 21, 2011 can you post an example of your arrays? like... echo "<pre>"; print_r($questions); print_r($assignment_questions); Quote Link to comment https://forums.phpfreaks.com/topic/228440-removing-elements-of-an-array-if-theres-something-in-common/#findComment-1177918 Share on other sites More sharing options...
kreut Posted February 21, 2011 Author Share Posted February 21, 2011 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/228440-removing-elements-of-an-array-if-theres-something-in-common/#findComment-1177937 Share on other sites More sharing options...
Veteah Posted February 21, 2011 Share Posted February 21, 2011 Ah, actually scratch that for the moment Quote Link to comment https://forums.phpfreaks.com/topic/228440-removing-elements-of-an-array-if-theres-something-in-common/#findComment-1177948 Share on other sites More sharing options...
.josh Posted February 21, 2011 Share Posted February 21, 2011 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) Quote Link to comment https://forums.phpfreaks.com/topic/228440-removing-elements-of-an-array-if-theres-something-in-common/#findComment-1177950 Share on other sites More sharing options...
kreut Posted February 21, 2011 Author Share Posted February 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228440-removing-elements-of-an-array-if-theres-something-in-common/#findComment-1177953 Share on other sites More sharing options...
.josh Posted February 22, 2011 Share Posted February 22, 2011 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') Quote Link to comment https://forums.phpfreaks.com/topic/228440-removing-elements-of-an-array-if-theres-something-in-common/#findComment-1177977 Share on other sites More sharing options...
kreut Posted February 22, 2011 Author Share Posted February 22, 2011 Thanks! Your SQL statement really helped me see what I needed to do... Quote Link to comment https://forums.phpfreaks.com/topic/228440-removing-elements-of-an-array-if-theres-something-in-common/#findComment-1178141 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.