Archive for the 'Sample Code' Category

Array_diff_assoc and array_diff_key Functions

The next array comparison functions is the array_diff_assoc(array1,array2,array3,array3…..), usage is similar with all of these array_diff functions varying only in the way the comparisons are done. Below is sample code for array_diff_assoc:

$a1=array(0=>“Mouse”,1=>”Cat”,2=>”Dog”);
$a2=array(0=>”Lizard”,1=>”Dog”,2=>”Cat”);
$a3=array(0=>”Dog”,1=>”Cat”,2=>”Mouse”);
print_r(array_diff_assoc($a1,$a2,$a3))
?>

Giving you : Array ([0] => Mouse [2] => Dog).

Next we have the array_diff_key() function compares two or more arrays and returns an array with the keys and values from the first array only if the key is not present in the other arrays. Syntax is array_diff_key(array1,array2,array3……)which is similar to the other array_diff functions.

Sample usage:
$a1=array(0=>“Mouse”,1=>”Cat”,2=>”Dog”);
$a2=array(2=>”Fish”,3=>”Rat”,4=>”Bee”);
$a3=array(5=>”Dog”,6=>”Cat”,7=>”Fish”)
print_r(array_diff_key($a1,$a2,$a3));
?>

Giving you : Array([0] => Mouse [0] => Cat)

Array_diff Function

The first function, array_diff() is used for comparing several tables or arrays which gives an array with the keys and values from the first array if the value is not available in the other arrays. Syntax is as follows : array_diff(array1,array2,array3……), where array 1 is the table to which all the other arrays will be compared to. The 2nd array(array 2) is an array that is compared with the first array and so on and so forth. Below is sample code of its use and the outcome of the functions comparison :

$a1=array(0=>“Mouse”,1=>”Cat”,2=>”Dog”);
$a2=array(3=>”Dog”,4=>”Cat”,5=>”Lizard”);
print_r(array_diff($a1,$a2));
?>

Giving you the result : Array( [0] =>Mouse )

Array Count Values

The array_count_values() function returns an array which contains the keys of the original array’s value and the value is the number of occurences. A sample of it’s use is shown below:

$a=array("Mouse","Cat","Dog","Cat");
print_r(array_count_values($a));
?>

Which would give us an output of : Array ( [Mouse]=> 1 [Cat]=> 2 [Dog]=> 1 )

The next functions are used to compare the contents of one array against one or more arrays either returning the key, keys and contents or solely the contents of the specified fields that result from their comparisons. They are array_diff(), array_diff_assoc(), array_diff_key(), array_diff_uassoc() and array_diff_ukey(). all of these are used to determine the difference between a set of arrays returning either the keys or contents to give the results of the said array comparison/s.

Array Chunk Function

The array_chunk() function on the on the other hand as the name implies, divides an array into chunks or several tables from the source table. The syntax goes something like array_chunk(array,size,preserve_key), wherein the array is the table that would be divided, the size is the number of elements which the new arrays are to contain and the preserve key which can either be true or false is used to either retain or revise the key or pointer value of the original table. An example is shown below:

$a=array(�a�=>�Cat�, �b�=>�Dog�, �c�=>�Horse�,�d�=>�Cow�);
print_r(array_chunk($a,2);
?>

The code would have an output of:

Array (
[0] => Array ( [0] = > Cat [1] => Dog )
[1] => Array ( [0] => Horse [1] => Cow )
)

As we can see, the original array has been divided into two arrays array0 and array1 and a value that is not given for the key had it assigned a new key for each of the tables. Another example would be :

$a=array(�a�=>�Cat�, �b�=>�Dog�, �c�=>�Horse�,�d�=>�Cow�);
print_r(array_chunk($a,2,true);
?>

This would then give us ;
Array (
[0] => Array ( [a] = > Cat [b] => Dog )
[1] => Array ( [c] => Horse [d] => Cow )
)

This shows the significance of the retain key field wherein the two new arrays retained their original keys. The reverse of which would be the array_combine() which divided the array into one which holds the keys and one with the values.

Embedding Comments

Now, to make you a better programmer we all know the value of comments. This allows you to understand the code that you have written defining and given meaning to operations as you build them up. You start with the terminators used by PHP and end with them as well. Single line comments look like this �// comment� and Multi-line ones use the syntax /* comment comment*/. A better example would be the one below:

//comment
/* comment
Comment*/
?>

In the next post we take on the best parts of PHP which would be variables which is essential in all programming languages.

More into the syntax of PHP

As you might have seen, all of the PHP statement ends with �;� which would be somewhat similar to Perl. The valid HTML code that was handed back to the server was :



Who are You?

My name is MacGyver.

More in the coming posts when we dig deeper as we widen our understanding of PHP.

Array Combine

This function combines two arrays where the first array is treated as the key and the second array as the contents of the said table. The syntax goes like this : array_combine(array1,array2), wherein the array1 is the table which contains the keys values, and the array2 is the contents. It should be noted that these two tables or array�s should have equal amounts of contents for it would become problematic if there was an error in the combination process. An example of the process is shown below:

$a1=array('a','b','c','d')
$a2=array2("Mouse","Rat","Rodent","Mice")
print_r(array_combine($a1,$a2))
?>

The output of which would give us a single table :

Array ( [a] => Mouse [b] => Rat [c] => Rodent [d] => Mice )

The function combined the two tables giving a single table that combines the keys and the contents. More in-depth discussion on the different array functions which in future posts would be the backbone of applications we will be building.

RSIs: Programmer’s Nightmare


Image source: www.basicphpprogramming.com

A programmer’s job requires him to keep his eyes focused on the glowing computer screen and keying in data by constantly pounding the keys of the keyboard. Programmers love what they do that it even makes them glued on their seats in front of their PCs which even they admit to be obsessive. But the consequence of their love affair with the computer is the painful overuse injuries that can keep them from doing their fist love—programming— and even simple tasks such as opening a mineral water bottle and turning the doorknob.
Repetitive Strain Injuries are acquired by the overuse of muscles and/or holding them in an unnatural position for a long time.
There are two common repetitive strain injury or RSIs are carpal tunnel syndrome and forearm tendonitis. If untreated, carpal tunnel syndrome can sometimes cause permanent nerve damage, and forearm tendonitis can harm muscles, tendons and ligaments that are essential for repetitive motion.
Standard keyboards require programmers to hold their hand in a fixed and unnatural position that can strain the forearms. Some computer tables may also be too high for them that when they key in data, their wrists are flexed which puts too much pressure on the median nerve. Constant pounding on the keys can cause tiny tears in the muscles and ligaments. In case no adjustments are made, this minor strain and discomfort can eventually lead into disabling injuries that can take months to heal. In some cases, even turning a doorknob produces extreme pain that is cause by hypersensinized nerves on the wrists.
Good news: RSIs are preventable and treatable. Regular breaks from typing, exercises concentrating on the hands, wrists and fingers, maintaining good posture in sitting and proper arm placement at the keyboard and a healthy workplace will go a long way towards preventing them.

Arrays

Arrays are what tables are to C-based programming languages and what databases are for SQL-based languages. Arrays or tables as they are sometimes called can be used to store the contents of several variables and to create one, you use the following syntax:

Array(key=>value)

The array in the syntax refers to the name of the array being created, the key is the index which is set automatically to a numeric character or string if none is specified. Value is the assigned value or content of the said array which can be seen easily in the following array creation example:

$b=array('z'=>‘Comedy”,’y'=>”Horror”,’x;’=>”Action”);
print_r($b);
?>

This piece of code would produce an output of :

Array ([0] => Comedy [1] => Horror [2] => Action)

More on array functions in the next posts.

Arrays : Changing cases

This form of array declaration allows one to change the case from uppercase to lowercase and vice versa. The syntax goes as follows:

array_change_key_case(array,case)

The array part, specifies which table or array to use and is a required field which is not the case with the key which is automatically assigned a value. An example of it’s use can be seen below:

$a=array('a'=>“Mouse”,’b'=>”Rat”,’c'=>”Rodent”,’d'=>”Cat”);
print_r(array_change_key_case($a,CASE_UPPER));
?>

The output of the said commands will be:
Array ( [A] => Mouse [B] => Rat [C] => Rodent [D] => Cat)

Another example of it’s use would be:

$a=array('a'=>“Mouse”,’B'=>”Rat”,’c'=>”Rodent”,’b'=>”Cat”);
print_r(array_change_key_case($a,CASE_UPPER));
?>

That returns the following values respectively:
Array ( [A] => Mouse [B] => Rat [C] => Rodent [D] => Cat)

In the next post, we would discuss an array function that divides a large array into several chunks of separate arrays.