php - in_array not finding present items from defined array -


i having issue in_array not seeming find value in array. array constructed of categories , information several tables formed (relatively!). purpose of code is, if row has correct value in $level (supplied in function), , category_id not found in array should added. if found in array should removed array altogether. have been through debugging , seems in_array never returning positive assume have made mistake in implementation here. see code below:

$sql = 'select c.category_id, cd.name, cp.level ' .        'from oc_category c ' .         'left join c_category_description cd ' .        'on cd.category_id = c.category_id ' .        'left join oc_category_path cp ' .        'on cp.category_id = c.category_id';  $categories = $mysqli->query($sql);  $i = 0; while($row = $categories->fetch_array()) {     if ($row['level'] == $level)     {         if (in_array($row['category_id'], $cat_level_1))         {             unset($cat_level_1[$i]);                 $i++;         }         else         {             $cat_level_1[] = $row['category_id'];             $i++;         }     }    }  

currently output (truncated 7 rows) is:

array (size=40)   0 => string '17' (length=2)   1 => string '18' (length=2)   2 => string '20' (length=2)   3 => string '24' (length=2)   4 => string '25' (length=2)   5 => string '26' (length=2) 

any ideas mistake have made appreciated!

sam

i believe problem unset($cat_level_1[$i]);

you setting $i start @ 0, , time find $row['category_id'] somewhere inside $cat_level_1 erase first entry, not actual entry matched $row['category_id'].

you want use array_search() function instead, return key found item, if set $i key code might have better results.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -