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 6988791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:07:39+00:00 2026-05-27T19:07:39+00:00

I have this little code which lists all matches of items that contains the

  • 0

I have this little code which lists all matches of items that contains the key that haz been searched for.

The code I currently have lists all thing no matter how many times it occurs in the array.

for ($i = 0; $i < count($return); $i++) {
    $herb = explode('<+>',$return[$i]);
    $inf = explode('<+>', $njdbData[$herb[0]]);
    echo '<div class="herbListItem" onclick="goToHerb(' . $herb[0] . ')">
          <img src="../img/herbs/' . $inf[2] . '-2.jpg" />    
          <span>'.$inf[0].'<i>'.$herb[1].'</i></span></div>';
}

I’ve made so that all item ids and why the item ids are listen are in an array.
For example, if I search for “Red dragon.”.

  • The item with id 1 got listed because I searched for “dragon”.
  • The item with id 3 got listed because I searched for “dragon”.
  • The item with id 4 got listed because I searched for “China”.
  • The item with id 1 got listed again because I searched for “red”.

Because there was 2 hits for item 1 I want item 1 to show up only once and on the top…

In the code herb[0] returns the id of the item and herb[1] returns why the item is in the array.

Can anyone help me making it so it echos out a list where, in the example, instead of echoing this out when searching for “Red dragon”:

  • Item 1, Because you searched for Dragon
  • Item 3, Because you searched for Dragon
  • Item 4, Because you searched for China
  • Item 1, Because you searched for red

it echoes out this:

  • Item 1, Because you searched for Dragon and red
  • Item 3, Because you searched for Dragon
  • Item 4, Because you searched for China

I hope you understand what I’m trying to achieve here.

Thanks in advance, enji

  • 1 1 Answer
  • 1 View
  • 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-05-27T19:07:40+00:00Added an answer on May 27, 2026 at 7:07 pm

    Add another array that keeps track of which ones have been echoed out.

    $used_ones= array();
    $pre_sort = array();
    for ($i = 0; $i < count($return); $i++) {
        $name =  explode('<+>',$return[$i]);
        array_push($pre_sort, $name[0]);
    }
    $sorted = array_count_values($pre_sort);
    arsort($sorted, SORT_NUMERIC);
    foreach($sorted as $key=>$val) {
       for ($i = 0; $i < count($return); $i++) {
            $herb = explode('<+>',$return[$i]);
            if($herb[0] == $key){
                $inf = explode('<+>', $njdbData[$herb[0]]); 
                echo '<div class="herbListItem" onclick="goToHerb(' . $herb[0] . ')"><img src="../img/herbs/' . $inf[2] . '-2.jpg" /> <span>'.$inf[0].'<i>'.$herb[1].'</i></span></div>';
                break;
            }
    
       }
    }
    

    Adding multiple reasons why a line was printed doesn’t seem possible with how you are doing this because the $inf variale gets overwrriten each time through the for loop. With some tinkering in your process you’ll be able to print out multiple reasons why a value was returned. But this should eliminate your muultiple responses.

    EDIT: Give this a shot. I’m not sure how the array_count_values will act because I don’t know what you have in $return[n]. Code has not been tested.

    EDIT#2: I’m an idiot and forgot to sort the array from High to Low based on value. That’s been added.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this little problem, that I cannot figure out which arguments to pass
I have this little question that's been on my mind for a while now.
I have taken this little snippet straight out of some code I'm working on:
its a little bit hard to understand. in the header.php i have this code:
I have a little problem. This is my code: stackPanelShapesContainer = new StackPanel(); InitializeStackPanle();
I have this little NetStream movie (which I'm not allowed to show, sorry) set
I have this little script that shows one wisdom each day. so I have
Hey, I've got this nice little piece of code, much like all the other
I have been working all night and yesterday on this header section in css,
I have a little piece of code which populates a CellTable from a Type

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.