0

I recently got this error on my error log:

   PHP Warning:  in_array() [<a href='function.in-array'>function.in-array</a>]: Wrong datatype for second argument in... on line 423

and refers to the following piece of code:

    <?php foreach($services_a as $key=>$service) { ?>
    <div class="oneservice">
    <input type="checkbox" name="services[]" value="<?php echo $key; ?>" id="service<?php echo $key; ?>"<?php if( in_array($key, $services) ) { echo ' checked="checked"'; } ?> />
    <label for="service<?php echo $key; ?>"><?php echo $service; ?></label>
    </div>

Any views are very welcome, Thanks

3
  • Is $services an array? You can use var_dump() to find out Commented Jan 22, 2012 at 19:26
  • It is defined as follows: $services = $_POST['services']; if ($services && is_array($services)) { foreach ($services as $i => $service) { $service = (int)$service; $array = array( 'key' => 'services', 'value' => '%"'.$service.'"%', 'compare' => 'LIKE' ); $meta_query[] = $array; unset($array); } } Commented Jan 22, 2012 at 19:28
  • $services_a = array( "1" => _d('Green)',263), "2" => _d('Blue',264), "3" => _d('Yellow',265), "4" => _d('Red',266), "5" => _d('Purple',267), "6" => _d('Red',268), ); Commented Jan 22, 2012 at 19:52

3 Answers 3

4

in_array() checks for the value, not the key.

Use array_key_exists() if xou want to check for the key:

<input type="checkbox" name="services[]" value="<?php echo $key; ?>" id="service<?php echo $key; ?>"<?php if( array_key_exists($key, $services) ) { echo ' checked="checked"'; } ?> />

When you open the form the first time, your $_POST['services'] will be empty. to overcome the error, initialize an empty array if nothing is coming from post:

$services = is_array($_POST['services') && count($_POST['services']) > 0 ? $_POST['services'] : array();
Sign up to request clarification or add additional context in comments.

3 Comments

PHP Warning: array_key_exists() expects parameter 2 to be array, null given in I get this error
I've added some code to initialize the $_POST['services'] if it's empty.
Will this be of any help $services_a = array( "1" => _d('Green)',263), "2" => _d('Blue',264), "3" => _d('Yellow',265), "4" => _d('Red',266), "5" => _d('Purple',267), "6" => _d('Red',268), );
0

You should check "an array" Is it an array?

<?php if(is_array($services)): ?>
 <?php if( in_array($key, $services) ) { echo ' checked="checked"'; } ?>
<? endif; ?>

1 Comment

$services_a = array( "1" => _d('Green)',263), "2" => _d('Blue',264), "3" => _d('Yellow',265), "4" => _d('Red',266), "5" => _d('Purple',267), "6" => _d('Red',268), );
0

Probably because you called the array you are iterating for $services_a but the array you use for second argument in in_array() is called plain $services. Problem is probably that the second argument has a NULL value, which gives you the warning.

Comments

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.