0

Possible Duplicate:
in_array() and multidimensional array

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

$skus = array();
$result = mysql_query($sql);
if($result){
  while($rows = mysql_fetch_array($result)){
      $skus[]=$rows;
  }
}

Results:

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?

Community
  • 1
  • 1
MB34
  • 4,210
  • 12
  • 59
  • 110

3 Answers3

1

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)

adam
  • 2,930
  • 7
  • 54
  • 89
  • 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
1

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;
    }
}
Rosme
  • 1,049
  • 9
  • 23
1

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)
RDK
  • 4,540
  • 2
  • 20
  • 29