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

Posted on 2012-08-30
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
    LVL 34

    Expert Comment

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

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


    Author Comment

    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 34

    Accepted Solution

    $categories = array();
    foreach ($matches[1] as $num => $value) {
    $tmp_category = $matches[5][$num];

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
    Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
    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…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now