here is the syntax for array creation,search ,loop from the array in php
******syntax to create and array in Php******
< ?php
$myarray = array(1,2,3,4,5,6,7,8,9,10);
or
$myarray = range(1,10);
or
$myarray = range(a,z);
? >
******syntax to view item of array******
< ?php
$values = array("a","b","c","d");
print_r($values);
? >
******syntax of loop foreach to print a to z******
< ?php
$alphabet = range("A","Z");
foreach($alphabet as $letter) { echo "The letter $letter\n"; }
? >
*******syntax to search item in array*******
< ?php
$values = array("a","b","c","d");
$newvalue = "cr";
if (in_array($newvalue,$values)) { echo "$value is already in the array!"; }
? >
******syntax for shuffle or random item from a to z******
< ?php
$values = range("A","Z");
shuffle($values);
print_r($values);
? >
******syntax for sorting in array******
< ?php
$myworld= range("A","Z","O","P","L","k");
arsort($myworld);
print_r($myworld);
? >
******syntax for multi-dimensional array******
< ?php
$multiarray = array(array(5,4,3,2,1),array(7,8,2,9,10));
array_multisort($multiarray[0],SORT_ASC);
print_r($multiarray);
? >
******syntax for get count of element in array ******
< ?php
$values = array(1,2,3,4,5,6,7,8);
$elements = array_unshift($values,1);
echo "No of elements: $elements \n\n";
print_r($values);
? >
******syntax for number of times a vallue appears in array ******
< ?php
$values = array(a,b,c,d,s,a,a,c,d,f,a,s,a,a,b,d,d,d);
$most_popular = array_count_values($values);
print_r($most_popular);
? >
******syntax to find size of an array *******
< ?php
$values = range("A","Z");
echo count($values);
? >
******syntax to find randome number from array ******
< ?php
$number = range(1,100);
$rannumber = array_rand($number);
echo "Number is: $rannumber\n\n";
? >
******syntax to remove first value from array ******
< ?php
$values = array(a,b,c,d,e,f,g);
$first = array_shift($values);
echo "First value is: $first";
print_r($values);
? >
******syntax to invert array ******
< ?php
$values = array("b","a","c");
print_r($values);
$values = array_flip($values);
print_r($values);
? > | |