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?

1

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

Comments

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)

1 Comment

Actually, I did it a different way. I used a where clause in the query and checked the numrows.
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;
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.