PHP Inserting new data into arrays if it doesn't exist.

Posted on 2012-08-30
Medium Priority
Last Modified: 2012-08-30
I have about 10 categories that change all the time the page is refreshed and therefore, every time the script is loading, I need to insert the category name into array if it does not exist with an integer 1, but if the category already exists in the array, I need to get the integer that represents how many items were counted in that category and increase it by one.

For example:

foreach loop:

category name = "CAT1" and it doesn't exist, so we add it to the array "CAT1" => "1" means it is the first time we see any item from this category.
now we do the same over and over again and if we see the item with category name CAT1, we should know that we already had one item from that category in the past, so we increase the amount in the array and now we have "CAT1" => "2".

Thank you for help.
Question by:VAL1N
  • 2
LVL 35

Expert Comment

ID: 38350917
$source = array("CAT1","CAT2","CAT3");

$categories = array();
foreach($source as $sourceCat)


Author Comment

ID: 38350951
I don't know the list of categories, so I need to add the categories into the array as I go through the foreach loop.

$categories = array();

foreach ($matches[1] as $num => $value) {

$tmp_category = $matches[5][$num];

//check if category exists or not, if not then add with integer 1, if does, then i++


so if $tmp_category does not exist in $categories, then we add it in and it should have 1 as integer in it so that I know later on, how many times that category had items in it.

but if $tmp_category already exists in $categories (because we added it earlier on in the foreach loop), then we just increase it's value by 1
LVL 35

Accepted Solution

gr8gonzo earned 2000 total points
ID: 38350962
$categories = array();
foreach ($matches[1] as $num => $value) {
$tmp_category = $matches[5][$num];

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question