Saturday, December 24, 2011

Searching and removing a value from a multidimensional array in php

Recently I found myself in a situation where I needed to search and remove a value from a dynamically generated multidimensional array with a different size and length.

After many trials and errors I found that the best solution is to recursively loop through the array and locate and remove the value.

So I wrote the following function, that takes two parameters, first is the array passed by reference and the second is the value we want to completely remove from the array:

function recursiveRemoval(&$array, $val)
{
    if(is_array($array))
    {
        foreach($array as $key=>&$arrayElement)
        {
            if(is_array($arrayElement))
            {
                recursiveRemoval($arrayElement, $val);
            }
            else
            {
                if($arrayElement == $val)
                {
                    unset($array[$key]);
                }
            }
        }
    }
}

First thing we're doing is checking if the array passed to the function is actually an array.
If the array passed is an array, we iterate through its elements,
If the element is also an array (our array is not a one dimensional array), we recursively call the function recursiveRemoval($arrayElement, $val) again to break the multidimensional array into one dimensional arrays.
At the end, we process the one dimensional array elements, and we compare them to the passed value we want to remove, and we unset the key of the array ($array[$key]).


To download a fully working example, please go to:
http://www.codeproject.com/KB/PHP/RecursivelyRemoveArrayVal.aspx

Monday, November 21, 2011

Understanding the Current Memory Usage in Linux

To check the overall memory usage on your linux, we can use the command: free
This will show the memory usage in KB (default),
If you want to check your memory usage in MB, enter the command free -m, in GB free -g , and in Bytes free -b


If you're new to linux, you will probably be wondering what does all that stuff mean, what's a buffer, what's a cache, what's a swap, what's the actual free memory on my machine...

Let's break it down row by row:
First Row (Total): This shows the total physical memory you have no your machine.
First Row (used): This is the overall allocated memory by the system.
First Row (free): This shows the free memory.
First Row (Shared): This shows the amount consumed by shared segments of code or data.
First Row (buffers): The amount of memory used as buffers. Buffers are feasible when a large block of data is assembled or disassembled (as required by a storage device), or when data may be delivered in a different order than that in which it is produced. Buffering sometimes increases transfer performance or reduces the variation or jitter of the transfer's latency.
First Row (cached): The amount of memory used as cache.  Cache can increase the data transfer performance because there is a good chance that the same datum will be read from cache multiple times, or that written data will soon be read. A cache's sole purpose is to reduce accesses to the underlying slower storage. For example, imagine having a simple webpage that shows information about a person, instead of accessing this page from the hard disk everytime a user visits this page, the cache will store it and deliver it to the user, and thus reducing the transfer speed.

Second Row (used):  This shows the data in memory that is being processed by the machine. The cache and buffers in First Row are just allocated by the system waiting to be used (and can be freed without causing any troubles, but of course it's better to keep them to increase performance).
Second Row (free): This is the free memory if the cache and buffers are deleted from the memory. You can calculate this using a simple formula:
Free (in Row 1) + buffers (row 1) + cached (row 1) = Free (row 2)
for example in the screenshot above: 962912 + 166848 + 726844 = 1856604

Third Row (total): Shows the total of swap memory or virtual memory available on the machine.
Third Row (used): Shows the amount of swap memory used, usually the swap is used when the physical memory is all allocated.
Third Row (free): Shows the free swap on the system.

Four Row (total): Total available memory on the machine.
Four Row (used): Total Memory currently in use.
Four Row (free): Total Free memory.