php - in_array() expects parameter 2 to be array, boolean given -
problem in_array() on php code. have following array:
array ( [0] => 11 [1] => 13 [2] => 14 [3] => 15 [4] => 16 [5] => 17 [6] => 18 [7] => 19 [8] => 20 [9] => 21 [10] => 22 [11] => 23 [12] => 24 [13] => 25 [14] => 26 [15] => 27 [16] => 28 [17] => 29 )
and following function removes element array (since unset not keep indexes):
function removefromarray($value, $array) { // if value in array if (in_array($value, $array)) { // key of value $key = array_search($value, $array); // remove element unset($array[$key]); // fix key indexes $array = array_values($array); return $array; } return false; }
unfortunately i'm getting error: "in_array() expects parameter 2 array, boolean given" when in_array($value, $array), if condition. happens whatever element of array.
i've made check is_array() on $array variable , returns true, variable recognized array. thoughts?
edit:
i define array follows:
$array = array(11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29);
and call function way: (for example: if want remove number 11)
$array= removefromarray(11, $array);
your code fine. not answer, it's show code fine.
i tested follows:
<?php function removefromarray($value, $array) { // if value in array if (in_array($value, $array)) { // key of value $key = array_search($value, $array); // remove element unset($array[$key]); // fix key indexes $array = array_values($array); return $array; } return false; } $array = array(11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29); $array= removefromarray(11, $array); var_dump($array);
the result:
[vinod@wiplnx01:/home/workspace/php/playground]$ php array_test.php array(17) { [0] => int(13) [1] => int(14) [2] => int(15) [3] => int(16) [4] => int(17) [5] => int(18) [6] => int(19) [7] => int(20) [8] => int(21) [9] => int(22) [10] => int(23) [11] => int(24) [12] => int(25) [13] => int(26) [14] => int(27) [15] => int(28) [16] => int(29) }
and php version, although should not matter:
[vinod@wiplnx01:/home/workspace/php/playground]$ php -v php 5.4.16 (cli) (built: jun 6 2013 09:20:50) copyright (c) 1997-2013 php group zend engine v2.4.0, copyright (c) 1998-2013 zend technologies xdebug v2.2.3, copyright (c) 2002-2013, derick rethans
so please check if have typos or somewhere.
Comments
Post a Comment