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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:33:16+00:00 2026-06-13T04:33:16+00:00

Say I have an a array like this: Array ( [0] => bananas, apples,

  • 0

Say I have an a array like this:

Array (
  [0] => "bananas, apples, pineaples"
  [1] => "bananas, show cones"
  [2] => ""
  [3] => "apples, santa clause"
..
)

From that array I want to create a new array, holding each “tag”, and the number of times it occuredd in the first array, preferebly sorted alphabetically,like this:

Array (
  [apples] => 2
  [bananas] => 2
..
)

or

array (
  [0] => [apples] => 2
..
)
  • 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-13T04:33:17+00:00Added an answer on June 13, 2026 at 4:33 am
    // array to hold the results
    $start = array( "bananas, apples, pineaples", "bananas, show cones", "", "apples, santa clause" );
    // array to hold the results
    $result = array();
    
    // loop through each of the array of strings with comma separated values
    foreach ($start as $words){
    
        // create a new array of individual words by spitting on the commas
        $wordarray = explode(",", $words);
        foreach($wordarray as $word){
            // remove surrounding spaces
            $word = trim($word);
            // ignore blank entries
            if (empty($word)) continue;
    
            // check if this word is already in the results array
            if (isset($result[$word])){
                // if there is already an entry, increment the word count
                $result[$word] += 1;
            } else {
                // set the initial count to 1
                $result[$word] = 1;
            }
        }
    }
    // print results
    print_r($result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say, I have an array that looks like this: var playlist = [ {artist:Herbie
Let's say I have an array that store like this: Array ( [0] =>
So, say I have an array that looks like this: t = [ [
In PHP, say that you have an associative array like this: $pets = array(
Say I have a function that returns an array like this... array( 0 =>
Let's say I have an array that looks like this: $array[0] = 'House'; $array[1]
Let's say I have an array like this: string x[2][55]; If I want to
Say I have an $input array, that contains something like this : array 0
Let's say you have an array that looks like this: $myArray []= array('firstname' =>
Say you have an ordered array like this, generated from a database of addresses:

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.