Archive for the ‘General’ Category
Marketing research lectures
The purpose of this post is to share my online lectures of the Marketing research course I have taken during the COVID 19 lockdown.
Lecture 1: How to design your marketing research problem?
Lecture 2: Descriptive research
Lecture 3: Secondary data sources
Lecture 4: Literature Review
Social media simulation- Mimic Social
Please find the following demonstration to know more about using Mimic Social- a social media simulation software from Stukent.
Functions to sort arrays in PhP
December 3, 2024
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…