Archive for June, 2014
Functions to sort arrays in PhP
Hi all,
Today I will list down the most common functions used to sort arrays in PhP. Hope this helps to keep in touch with various functions needed to sort your array related assignments.
functions | what does this mean? |
---|---|
array_multisort() | Sort multi dimensional arrays |
arsort() | sort an array in reverse order and maintain index association |
asort() | Sorts an array and have index association |
ksort | Sorts an array by key |
krsort | Sort an array by its keys in reverse order |
rsort() | Sort an array in reverse order |
Shuffle() | Shuffles an array |
uksort() | Sort an array by keys by using a user defined comparison function |
usort() | Sort an array by values by using a user defined comparison function |
[<?php
$names=array(“d”=>”Arun”,”e”=>”Bimal”,”f”=>”Chris”);
asort($names);
foreach($names as $key=>$value){
echo “$key=$value </br >”;
}
?>]
The above scripts will sort the arrays in the alphabetical order and will show out put as
d=Arun
e=Bimal
f=Chris
On the other hand, if we use ARSORT(), we can reverse the list according to the values. Try the following.
[<?php
$names=array(“d”=>”Arun”,”e”=>”Bimal”,”f”=>”Chris”);
arsort($names);
foreach($names as $key=>$value){
echo “$key=$value </br >”;
}
?>]
The output will be:
f=Chris
e=Bimal
d=Arun
Now lets sort the array by key.
[<?php
$names=array(“d”=>”Kristopher”,”e”=>”Larry”,”f”=>”Micheal”);
ksort($names);
print_r ($names);
?>]
Here we use “KSORT() to sort the values by its indices. Lets see the output.
Array ( [d] => Kristopher [e] => Larry [f] => Micheal )
Now we can reverse this order by using KRSORT() and the new output will be:
Array (
[f] => Micheal
[e] => Larry
[d] => Kristopher )
SHUFFLE()
This function helps to shuffle the contents of an array.
[<?php
$num = array(1,2,3,4,5);
Shuffle($num);
foreach($num as $key=>$value){
echo “$key=$value <br />”;}
?>]
The output will show the keys and its values:
0=4
1=2
2=1
3=5
4=3
Everytime you refresh the browser, the values for each of the arrays will change.
Thats all for today… I will publish more functions related to use of array later.
Thanks for reading..
Surej
Creating Arrays in PhP
PHP is regarded as the most popular server side scripting language. Since its origin in 1995, PHP is growing every day due to its characteristics such as its very simple to learn and use, very fast, its free, its a portable XHTML-embedded scripting language, well suited for designing and managing database enabled web-pages. I am not going to talk everything about PHP (though I wish to) today in this post. I love to share some of my thoughts about various functions used in PHP for creating, modifying, checking, sorting arrays. My intention is to help those learning this wonderful language as much as I can by giving some explanations and simple examples.
Function used for Creating Arrays:
According to the PHP Manual, an array is an ordered map where a map is a type that maps values to keys. In simple words, an array is combination of key-value pairs. The key or an index of an array can be a number or a string or both which is used to identify the corresponding value in the key. The value of an array is known as element which can be of any datatypes. There are two types of arrays; a numeric array which is indexed by a number and an associative array which is indexed by a string.
Following are the functions used to create an array:
array | What does this mean? |
---|---|
array() | Creates an array |
array_combine() | Creates a new array[3] after combining 2 arrays. The new array will have the keys of array[1] and values of array[2] |
array_fill() | Fills an array with values. |
array_pad() | Pads an array to the specific length set by user with a value |
range() | Creates an array containing a range of values |
Apart from this, another method is just use array identifier[] and PHP automatically creates a numeric array with index starting 0.
Here are some examples for the above functions.
$names=array (‘John’, ‘Surej’);
This is a numeric array. Here $names[0]=John and $names[1]=Surej
$my_colors=array (2=>’red’, 3=> ‘green’);
This is a numeric array and key specified. Here $my_colors[2]=red and $my_colors[3]=green.
$Job=array (‘Dept’=>’Marketing’, ‘position’=> ‘Lecturer’);
This is an associative array. here $job[Dept]=Marketing and $Job[position]=Lecturer
echo $nums= range(1,10);
The above array will output 1 2 3 4 5 6 7 8 9 10 since the function asked to show numbers from 1 to max.10
echo $numb= range(1,10,2);
This function will output 1 3 5 7 9 since the function asked to show numbers from 1 to 10 with an increment of 2.
$love =array_fill(1,5,”I LOVE YOU”);
Array_fill function fills the array named love with index starting from 1 and values for all five arrays as I LOVE YOU.
ie $love[1]=$love[2]=$love[3]=$love[4]=$love[5]=I LOVE YOU.
If you need to see the elements of an array, you can use print_r() and var_dump() functions.
Print_r() | Displays all the elements(values) of an array |
var_dump() | Displays the array, the number of elements in it, and the length of each string values. |
There are many other functions which are used in PhP for various applications related to arrays. For example, to check whether the given key or index exists in an array, we use array_key_exists() or to check if the value exists in array, we use in_array() function. I will post some of those functions and its explanations in the next post. Thanks for reading…
May-The month of Blessed Mary
Traditionally,catholic church dedicate the month of may for Blessed Virgin Mary, mother of our lord, Jesus Christ. Except for the first few days, I was blessed by lord to spend some time for praying to mother Mary through out this month. I am very happy to say that through out those days, i had a kind of protection from someone strange(you may not believe) from all kind of threats and accidents in my daily life.
I started those prayers with an intention of getting a thing done in my life. But as the days passed, I got the belief that our lives are more valuable to god and he knows what we need and what we deserve. Even if we dont ask, our lovely father in heaven will give us more than we require IF we follow his commands and we care him and love him. Every day of the month of May, I had to read some prayers and I heard about various incidents happened all over the world through the intercession of mother Mary.
It is the God himself who gave her as our mother and as sinners, we are not worthy to ask anything to god directly. But as a human and the mother of the SON OF GOD, Mary stands for us for all our help and needs.
I write this just for some of my friends who believe in Christ, but not yet ready to worship his mother. I just want to ask you that how can you forget the love of a mother. Which son will love you if he knows that you don’t like or respect his mom.
In my home, I have a small statue of mother Mary which is a constant companion of mine for past 15 years. Through out my bachelors, masters and doctoral degree study time, it was there with me. Wherever i lived and traveled, I could take it with me and now it shines in my living room blessing my small family. Whenever I look at her, I feel like she is smiling at me. I believe that it is mother Mary whom we should pray everyday for keeping us safe from all diseases and dangers.
This world is currently loving more violence , terrorism and adultery than a happy and a peaceful life. It is really the time we should change our lifestyle and pray to god and ask for forgiveness and pardon for our sins and lead a life where no more violence , hatred and killing each other.
With love and prayers…..
Surej