Take the 2-minute tour ×

Possible Duplicate:
in_array() and multidimensional array

Got the following array returned from a database using this code:

$skus = array();
$result = mysql_query($sql);
  while($rows = mysql_fetch_array($result)){


Array (
    [0] => Array {
            [0] => PUBELI
            [group_sku] => PUBELI
    [1] => Array (
            [0] => PUBESSENTIALS
            [group_sku] => PUBESSENTIALS
    [2] => Array (
            [0] => PUBMGRPROGROUPED
            [group_sku] => PUBMGRPROGROUPED
    [3] => Array (
            [0] => PUB25GROUPED
            [group_sku] => PUB25GROUPED

I'm looking for this value using in_array:

if (in_array('PUBESSENTIALS', $skus))

and it returns false. Am I doing this correctly?

Why would the array values not be enclosed in quotes if the values in the DB are strings?

share|improve this question

marked as duplicate by drew010, Baba, Jocelyn, kapa, ЯegDwight Oct 15 '12 at 22:12


Look into php.net/manual/en/function.array-walk.php –  Hast Oct 15 '12 at 19:10

3 Answers 3

up vote 1 down vote accepted

Don't use PHP if you may do something with MySql! Try this solution:

$sql = "SELECT * FROM table WHERE str = 'PUBESSENTIALS'"; // some query just add WHERE

$result = mysql_query($sql);
if($result) $Row = mysql_fetch_array($result)
share|improve this answer

Assuming $skus is the full array shown above, then 'PUBESSENTIALS' would not be in $skus, because $sku's contains child arrays.

However, in_array('PUBESSENTIALS', $skus[1]) would return true.

try looping through each $skus element, and then checking that child element for in_array(value, childArray)

share|improve this answer
Actually, I did it a different way. I used a where clause in the query and checked the numrows. –  MB34 Oct 15 '12 at 19:09

You are only looking into the first array, and not any other array. You should look through each to test every sub-array. Something like this could do the job:

foreach($skus as $sku) {
    if (in_array('PUBESSENTIALS', $sku)) {
        return true;
share|improve this answer
