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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:10:52+00:00 2026-06-05T15:10:52+00:00

I have an array where I fill in the types as string of each

  • 0

I have an array where I fill in the types as string of each element. For example:

Types array

type1 | type2 | type2 | type3 | type2 | type1 | type3

$types = array('type1', 'type2', 'type2', 'type3', 'type2', 'type1', 'type3')

Now I want to count the occurrence of each type as I iterate the array.

For example:

when I am at the first element of the array I want to return:

type1 : 1
type2 : 0
type3 : 0

When I am at the fourth element I want:

type1 : 1
type2 : 2
type3 : 1

Actually, I am only interested to find the occurrence of the type of the element which I am looking. For example: fourth element

type3: 1

Is there a php function to do that? Or I will have to iterate the whole array and count the occurrences of types ?

Thanks

  • 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-05T15:10:53+00:00Added an answer on June 5, 2026 at 3:10 pm

    There is not a native function to do this. But we can write a simple one:

    $items = array(
            'type1',
            'type2',
            'type2',
            'type3',
            'type2',
            'type1',
            'type3'
        );
    
        foreach ($items as $order => $item) {
            $previous = array_slice($items, 0, $order + 1, true);
            $counts = array_count_values($previous);
    
            echo $item . ' - ' . $counts[$item] . '<br>';
        }
    

    This code produces this:

    type1 - 1
    type2 - 1
    type2 - 2
    type3 - 1
    type2 - 3
    type1 - 2
    type3 - 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to have a MATLAB array fill a column with numbers in increments
I have a few html image tags that fill an array. On the page
I have array of strings, String[] data and it's 10 elements has value P
An array is defined of assumed elements like I have array like String[] strArray
I am trying to have name of contacts in one array and their types
I have a 9x9 2D array. I fill the array with random numbers and
is it possible to use std::fill to initialize an array of non-POD types? The
I have written a small webservice[Axis2/Java] which exposes a method public String Fill(String cacheName
I want an array of imageviews, but I don't know how to fill it
I have a problem where I'm trying to fill an array with 30 random

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.