Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8631011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:09:26+00:00 2026-06-12T09:09:26+00:00

Possible Duplicate: !in_array use in PHP – conditional statments I have this code that

  • 0

Possible Duplicate:
!in_array use in PHP – conditional statments

I have this code that is doing opposite.

Let’s assume I have array of 1,2,3,3,4,5

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

    $item = $row['item'];
    echo $item . '<br />'; // for testing
    $items[] = $row['item'];
    if (!in_array($item, $items)) {

        $output[] = $row;

        foreach ($items as $item) {
            echo 'Array thus far: ' . $item . '<br />';  // for testing
        }
    } 

}

Basically I want the array to not have duplicates. The test code should eventually print out 1,2,3,4,5.. but it actually print out 1,2,3,3,4,5 the in_array() function doesnt seem to work here with a varible?

Here is real output:

Avengers
Array thus far: Avengers
Avengers
Array thus far: Avengers
Array thus far: Avengers
American Dad!
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
American Dad!
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Christopher Columbus
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Avatar
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Kung Pao Chicken
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
The Brak Show
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
Array thus far: The Brak Show
Avengers
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
Array thus far: The Brak Show
Array thus far: Avengers
The Brak Show
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
Array thus far: The Brak Show
Array thus far: Avengers
Array thus far: The Brak Show
Space Ghost: Coast to Coast
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
Array thus far: The Brak Show
Array thus far: Avengers
Array thus far: The Brak Show
Array thus far: Space Ghost: Coast to Coast
Battlestar Galactica (2004)
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
Array thus far: The Brak Show
Array thus far: Avengers
Array thus far: The Brak Show
Array thus far: Space Ghost: Coast to Coast
Array thus far: Battlestar Galactica (2004)
Potstickers
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
Array thus far: The Brak Show
Array thus far: Avengers
Array thus far: The Brak Show
Array thus far: Space Ghost: Coast to Coast
Array thus far: Battlestar Galactica (2004)
Array thus far: Potstickers
Potstickers
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
Array thus far: The Brak Show
Array thus far: Avengers
Array thus far: The Brak Show
Array thus far: Space Ghost: Coast to Coast
Array thus far: Battlestar Galactica (2004)
Array thus far: Potstickers
Array thus far: Potstickers
Avatar
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
Array thus far: The Brak Show
Array thus far: Avengers
Array thus far: The Brak Show
Array thus far: Space Ghost: Coast to Coast
Array thus far: Battlestar Galactica (2004)
Array thus far: Potstickers
Array thus far: Potstickers
Array thus far: Avatar

There are repeat items in here I am trying to keep out of array.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:09:27+00:00Added an answer on June 12, 2026 at 9:09 am

    You need to push $row['item'] onto $items after you do the in_array():

    while ($row = mysql_fetch_assoc($result)) {
        $item = $row['item'];
        echo $item . '<br />'; // for testing
        if (!in_array($item, $items)) {
            $output[] = $row;
            foreach ($items as $item) {
                echo 'Array thus far: ' . $item . '<br />';  // for testing
            }
            $items[] = $row['item'];
        }
    }
    

    Or you can simplify it by removing $items:

    while ($row = mysql_fetch_assoc($result)) {
        $item = $row['item'];
        echo $item . '<br />'; // for testing
        if (!in_array($item, $output)) {
            $output[] = $row;
            foreach ($items as $item) {
                echo 'Array thus far: ' . $item . '<br />';  // for testing
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Are PHP short tags acceptable to use? <?=$num2?> $num2 is calling data
Possible Duplicate: How do I use arrays in C++? I have no idea how
Possible Duplicate: Is it okay to use array[key] in PHP? Is there any significant
Possible Duplicate: PHP Sort a multidimensional array by element containing date I have some
Possible Duplicate: Can I use operators as function callback in PHP? Is it possible
Possible Duplicate: How can I use PHP closure function like function() use() on PHP
Possible Duplicate: Cannot use concatenation when declaring default class properties in PHP? The following
Possible Duplicate: “Cannot use string offset as an array” error <?php $marks = 0;
Possible Duplicate: Output (echo/print) everything from a PHP Array I have done a query
Possible Duplicate: Should I use php quote escapes for single quotes or use double

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.