1

I have an array below and I need to access the checkbox3 array and find out if it is set.

Something like this but it doesn't work:

<?php if(isset($fields['checkbox3']["One"])): ?>
    One is set
<?php endif; ?>

array(2) {
  ["checkbox2"]=>
  array(1) {
    [0]=>
    string(10) "Don't Show"
  }
  ["checkbox3"]=>
  array(5) {
    [0]=>
    string(3) "One"
    [1]=>
    string(3) "Two"
    [2]=>
    string(5) "Three"
    [3]=>
    string(4) "Four"
    [4]=>
    string(5) "Five"
  }
}

4 Answers 4

3
in_array("One", $fields["checkbox3"]);

Use in_array().

bool in_array ( mixed $needle , array $haystack [, bool $strict ] )
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mike, you got it right quickest, so I'll accept your answer in 6 minutes
2

$fields['checkbox3']["One"] doesn't exist, rather "One" is the value of $fields['checkbox3'][0]

Comments

2

You want to use the in_array() function for that:

if (in_array("One", $fields["checkbox3"])) {

It looks up if a string exists as entry in the array. So you don't have to know the index or traverse the array yourself.

Comments

1
<?php if(isset($fields['checkbox3'][0])): ?>
    One is set
<?php endif; ?>

or use

in_array()

1 Comment

Sorry, forgot to mention, the array size will fluctuate

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.