1

I want to see how I can use !in_array.

I have this code, but it doesn't work:

while ($row = mysql_fetch_assoc($result)) {

        if (!in_array($row['item'], $output)) {
            $output[] = $row;
        }

      }

   print(json_encode($output));  // need all rows, not just ['item']

Yes, I knowI am using older mysql and the code is not very "safe". Just want to know about to check if an item is in an array or not. IF it is not, I want it to store. If it is, I want the loop to skip...

EDIT: This is an additional idea:

$items[] = "";
while ($row = mysql_fetch_assoc($result)) {
    $item[] = $row['item'];

        if (!in_array($row['item'], $items)) {
            $output[] = $row;
        }
 }
print(json_encode($output));

This code puts all items in the array even duplicates; I'm doing this to prevent dulicates

My SQL:

    $result = mysql_query("
    SELECT b.item, a.rate_id, a.review, c.category, u.username
    FROM comments a
    INNER JOIN items b
        ON a.item_id = b.items_id
    INNER JOIN master_cat c
        ON c.cat_id = b.cat_id
    INNER JOIN users AS u
        ON u.user_id = a.user_id
    ORDER BY rate_id DESC LIMIT 15;");
4
  • 5
    since your $output array seems to be filled with rows from the database, why not just change the query to include only distinct item column values? Commented Oct 4, 2012 at 15:31
  • I think you spotted the reason on this. I updated code slightly more also. It seems I need two different arrays? one to check items array and one to store data if the check passes? Commented Oct 4, 2012 at 15:35
  • I still dont see why you cant apply the criteria for the subset in the Query to begin with. Commented Oct 4, 2012 at 15:40
  • @Gordon, for what it's worth I added my query and also a new idea of what I am trying to explain. Commented Oct 4, 2012 at 15:42

2 Answers 2

3
  while ($row = mysql_fetch_assoc($result)) {

    if (!in_array($row['item'], $output)) {
        $output[] = $row['item'];
    }

  }

or

  while ($row = mysql_fetch_assoc($result)) {

    if (!in_array($row['item'], $output))  continue;
    $output[] = $row['item'];

  }
Sign up to request clarification or add additional context in comments.

3 Comments

I think I see what I am doing here. I am doing a print(json_encode($output)); and need all data of $row; not just ['item']. I need to make a second array of just items?
have a look at jonnyynnoj's answer
hmm... not working. I have adjusted my code to explain more of the concept I am looking for.
0

Looks like you want something like:

while ($row = mysql_fetch_assoc($result)) {

   if (!isset($output[$row['item']])) {
     $output[$row['item']] = $row;
   }

}

1 Comment

Hm... thanks but not working out. I added more code up top of what I am trying?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.