Setting All Values in an Array to Blank

Posted on 2011-04-25
Last Modified: 2012-08-13
Is there an easy and simply way to set all values in an array to blank?
Question by:deharvy
    LVL 107

    Expert Comment

    by:Ray Paseur
    Uhh, sure - use foreach and set them to a blank.
    LVL 107

    Expert Comment

    by:Ray Paseur
    What do you mean by blank?  Unset, NULL, a single character space?   Give us a little more to go on and we might be able to get you a better answer.

    Author Comment

    I definitely do not want to unset, so setting to null is more of what I want.

    LVL 107

    Accepted Solution

    OK, the code construct would look something like this:

    foreach($array as $key => $value)
        $array[$key] = NULL;
    LVL 2

    Assisted Solution

    Using array merge


    $a = array("a" =>"Dog","b"=>"Cat","c"=>"Horse");

    $myarray = array_merge(array_diff($a, $a));

    LVL 107

    Expert Comment

    by:Ray Paseur
    There are some other interesting things you can do with arrays.  PHP has lots of functions, and all of them are documented on the web site, with examples and user-contributed notes.   Have a look at the functions listed in the left sidebar here:

    Author Closing Comment

    Thanks for your help!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now