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

  • Home
  • SEARCH
  • 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 693635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:46:27+00:00 2026-05-14T02:46:27+00:00

I have a set of values that I’m pushing into an array in the

  • 0

I have a set of values that I’m pushing into an array in the order they occur

$valsArray = array(); 

//I process each value from a file (code removed for simplicity) 
//and then add into the array 
$valsArray[] = $val; 

How do I turn this into an associative array instead where the value gets inserted (as $key of associative array) only if it doesn’t exist. If it does exist increment its count ($value of associative array) by 1. I’m trying to find a more efficient way of handling those values compared to what I’m doing now.

  • 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-05-14T02:46:27+00:00Added an answer on May 14, 2026 at 2:46 am

    As you loop thru your values you can do the following:

    isset( $valsArray[$val] ) ? $valsArray[$val]++ : $valsArray[$val]=1;
    

    example:

    $valsArray=array();
    
    $val="foo";
    isset($valsArray[$val])?$valsArray[$val]++:$valsArray[$val]=1;
    $val="foo";
    isset($valsArray[$val])?$valsArray[$val]++:$valsArray[$val]=1;
    $val="bar";
    isset($valsArray[$val])?$valsArray[$val]++:$valsArray[$val]=1;
    
    print_r($valsArray);
    

    will get you:

    Array ( [foo] => 2 [bar] => 1 ) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that produces a set of primary key values that I
I have a form action that needs to have its value set from a
I have a set of values that are present on the page loading: eg
I have a set of enumeration values that I need to convert to text
I have a set of enumeration values that have 138 values. Something like: type
I have a class representing a set of values that will be used as
I have an activity that has properties that look something like this (they're set
I am trying to set Session values using a IHttpModule. I have set the
Suppose I have a set of values, stored in a std::set: {1, 2, 6,
I have a set of five boolean values. If more than one of these

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.