1

I'm certain this is VERY simple, but being new and not knowing what I'm doing I can't quite get it on my own.

I am trying to create an array of wordpress post ID's that is derived from a simple foreach loop.

Basically I have this as my code:

$omit_these_quizzes = array();    
foreach ( $filtered_pass as $single_quiz ) {
    $quiz_id_number =  $single_quiz['quiz'];
    $omit_these_quizzes[] = $quiz_id_number;
}

I would like to take each of the resulting $quiz_id_number 's and have then end up in array that looks like this:

$omit_these_quizzes = array(8195,8193);

However, I keep ending up with an array that only contains the variable from the very last foreach loop, instead of them all. What am I doing wrong?

Thanks!

2
  • Your code looks fine, so either $filtered_pass contains only one id, or the result in $omit_these_quizzes is okay, but you inspect/process it in the wrong way. Commented Nov 11, 2013 at 8:30
  • I believe your code is supposed to work correctly, but you can try this. php.net/manual/en/function.array-push.php Commented Nov 11, 2013 at 8:31

4 Answers 4

1

Your code looks fine.But if you want alternative try this:
see here for in_array and array_push.

$omit_these_quizzes = array();    
foreach ( $filtered_pass as $single_quiz ) 
{
   $quiz_id_number =  $single_quiz['quiz'];
   if(!in_array($quiz_id_number ,$omit_these_quizzes))
   {
     array_push($omit_these_quizzes, $quiz_id_number);
   }
}

hope it's help you.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I guess the if statement prevents my array from doubling up on id's? That seems to keep things clean and tidy. Thanks for the added bonus :-)
The if statement that you threw in helped me out tremendously with another piece of code. Double thanks to you.
you always welcome and yes the if statement prevents duplicate entry.
1

This work?

$omit_these_quizzes = array();    
foreach ( $filtered_pass as $single_quiz ) {
    $quiz_id_number =  $single_quiz['quiz'];
    array_push($omit_these_quizzes, $quiz_id_number);
}

1 Comment

Thanks! It does exactly what I was hoping for :-)
1

Looks like you are creating a new array in each loop, that's why you end up with an array containing just a single element... Use:

$omit_these_quizzes = array();    
foreach ( $filtered_pass as $single_quiz ) 
{
    $quiz_id_number =  $single_quiz['quiz'];
    array_push($omit_these-quizzes, $quiz_id_number);
}

1 Comment

Thanks! This code did exactly what I was looking for. Thanks for explaining why my old one wasn't working ^_^
0

Your code is correct. Inspect your $filtered_pass variable by calling var_dump($filtered_pass); may be it contain only one subarray.

1 Comment

I did check my $filtered_pass array, although I was using return print_r to check instead of var_dump like you suggested. Is var_dump the 'correct' way of checking? There were indeed multiple sub-arrays btw.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.