PHP (Array & Function)

Introduction of Array
Array is a data structure that is used to store similar types of values in a single variable and in PHP we can create any array with help of a predefined keyboard "array". 

For example if you want to store roll number of 100 students then instead of defining 100 variables it’s easy to define an array with 100 lengths.

Let's have an example of array
$a=array[100]

Explanation: in the above program "$a" is an array variable, "array" is a keyword that is used to define an array in PHP and "100" its maximum size.

Features of Array
1. Every array store its each value in by a unique index number. 

2. Array's first value always store in zero index, for example in "a[4]" array capable to store maximum 4 values and its index number define as following representation
a[0]=40
a[1]=60
a[2]=75
a[3]=70 

Types of Array
1. Numeric array An array with a numeric index. Values are stored and accessed in linear fashion.These arrays can store numbers, strings and any object but their index will be represented by numbers and by default array index starts from zero.

A Simple Example of Numeric Array
<?php
$a = array(1, 2, 3, 4, 5);
print_r ($a);
?>

Program ExplanationIn the above program “$a” is name of array  and “array” is PHP keyword (class) that is used to declare an array in PHP.

Note: Print_r (this command is used to print array’s values with index)

Print array's values without index number
Array's values can be print without their index number by echo command and for loop.

Program to print array content by echo command
<?php
$a=array(40,20,35,70,45);
for($i=0; $i<5; $i++)
{
echo "<br>$a[$i]";
}

?>

Program to find largest value
<?php
$L=0;
$a=array(40,20,35,70,45);

for($i=0; $i<5; $i++)
{
echo "<br>$a[$i]";
if($L < $a[$i])
{
$L=$a[$i];
}
}
echo"<br>Largest value is: ".$L;

?>

2. Associative array The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string so that you can establish a strong association between key and values.

An Example of Associative Array
<html>
<body>
<?php
/* First method to associative create array. */
$salary = array(
                           "A" => 2000,
                           "B" => 1000,
                           "C" => 500
                          );

echo "Salary of Mr. A is: ". $salary['A'] . "<br />";
echo "Salary of Mr. B is: ".  $salary['B']. "<br />";
echo "Salary of Mr. C is: ".  $salary['C']. "<br />";

/* Second method to create associative array. */
$color['r'] = "Red";
$color['g'] = "Green";
$color['b'] = "Blue";

echo "First color is: ". $color['r'] . "<br />";
echo "Second color is: ".  $color['g']. "<br />";
echo "Third color is: ".  $color['b']. "<br />";
?>
</body>

</html>

3. Multidimensional array - An array containing one or more arrays and values are accessed using multiple indices

Foreach Loop In PHP
The foreach loop works only on arrays, and it is used to loop through each key/value pair in an array.
Syntax:
foreach ($array as $value) {
    code to be executed;
}

In foreach loop every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.
An example of foreach loop
<?php
$a = array(1, 2, 3, 4, 5);

foreach($a as $b)
{
print "<br>".$b;
}
?>
Note: The first syntax form loops over the array given by array_expression. Every loop the value of the current element is assigned to $b. The internal array pointer is increased by one to the next element. The foreach loop runs for the all elements of an array.

Functions in array
Count: this function display total number of array elements.
Example
<?php
$a = array(1, 2, 3, 4, 5);
 echo "Total numbers in array: ".count($a);
?>

Sort: this function is used to arrange array’s element in ascending order.  
Example
<?php
$num=array(4,6,2,22,11);
sort($num);
print_r($num);
?>

rsort: Arrange array elements in descending order.
arsort: This function is used to arrange array’s elements in descending order.
Example
<?php
$num=array(4,6,2,22,11);
arsort($num);
print_r($num);
?>

array_sum: This function returns sum of all elements of an array.
Example
<?php
$num=array(4,6,2);
$sum=array_sum($num);
echo "Sum is: ".$sum;
?>

array_merge: this function is used combine two or more arrays in a new array.
Example
<?php
$a=array(23,5,7);
$b=array(7,12,10);
$c=array_merge($a,$b);
print_r($c);
?>

array_reverse: this function is used to reverse a selected array.
Example
<?php
$a=array(1,2,3,4,5);
print_r (array_reverse($a));
?>

String Array Functions In PHP
Strlen: By sing this function we can get the length of string.
Example
<?php
$name="Mukesh kumar";
$c=strlen($name);
echo "Total number of characters:".$c;
?>

Strtolower: Converts all characters of a string into lower case.
Example
<?php
$name="Mukesh kumar";

echo strtolower($name);
?>

Strtoupper: Converts the characters of a string into upper case.
Example
<?php
$name="Mukesh kumar";
echo strtoupper($name);
?>

Ord: we can get the ASCII value of input value.
Example
<?php
$char="A";
echo "ASCII value is:".ord($char);
?>

Chr: To get the character from ASCII value.
Example
<?php
$num="66";
echo "ASCII value is:".chr($num);
?>

Strcmp:  this function is used to compare two strings and returns zero, If both are same else it return a non-zero value.
Example
<?php
$a="Computer";
$b="Computer";

$c=strcmp($a,$b);

if($c==0)
{
   echo "Both strings are same";
}
else
echo "String are different";
?>

Strchr: by using this function we can get the characters of a string form specified character.
Example
<?php
$a="Computer";
echo strchr($a,"ut");
?>

Strstr: it same as strchr.
Strpos: to get the position of a character in a string and by default counting start by zero position.
Example
<?php
$a="Computer";
echo strpos($a,"C");
?>

Current: this function is used to show first element of an array.
Example
<?php
$a=array(13,2,12,4,5);
echo end($a);
?>
next() - moves the internal pointer to, and outputs, the next element in the array
prev() - moves the internal pointer to, and outputs, the previous element in the array.
end() – this function is used to show the last element of an array.

Str_replace: this function is used to replace string with new string.
Example
<?php
echo str_replace("world","Peter","Hello world!");
?>

Str_repeat: repeat string with specified number of times.
Example
<?php
echo str_repeat("world",5);
?>








No comments:

Post a Comment