What is array function in php?
count()
The count( ) function is used to return the length of an array.
count() Funtion Example.
File Name:- index.php
$books = array("java",".net","php");
echo "length of the array = " .count($books);
?>
Output :-
Loop through an indexed array :-
File Name:- index.php
$cars = array("volvo","bmw","wagonr","toyata");
$arr_length = count($cars);
for($x = 0; $x<$arr_length; $x++)
{
echo $cars[$x];
}
?>
Output :-
Array_rand() function :-
The array_rand() function returns a random key from an array, or it returns an array of random keys if you specify that the function should return more than one key.
Syntax :
array_rand(array,number)
$a=array("One","Two","Three","Four","Five","six");
$random_keys=array_rand($a,3);
echo $a[$random_keys[0]]."
";
echo $a[$random_keys[1]]."
";
echo $a[$random_keys[2]];
?>
output:
Two
Four
One
Example 1
Return a random key from an array:
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
print_r(array_rand($a,1));
?>
output:-
c
Example 2
Return a random key from an array:
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
print_r(array_rand($a,2));
?>
output:-
Array ( [0] => a [1] => b )
How to get random value from array.
$mplace = array('india','pakistan','USA','London','Japan','Australia','Perish','Dubai','china','kanada');
$pla = rand(0, count($mplace)-1);
$m_place = $mplace[$pla];
Example 3
$ran = array(1,2,3,4);
$k = array_rand($ran);
$v = $ran[$k];
echo $v;
/*********************/
$ran = array(1,2,3,4);
$randomElement = $ran[array_rand($ran, 1)];
output:-
mt_rand Example
$a = ['http://gmax.com/', 'http://google.com/', 'http://itechtuto.com/'];
$website = $a[mt_rand(0, count($a) - 1)];
?>
output:-
how to generate random no. :-
$ran = rand(1,100);
echo $ran;
/**************** OR ***********/
$ran1 = mt_rand(1,1000);
echo $ran1;
?>
how to get random value from array. :-
$ar = array('mahtab','habib','alam','kalam','asif');
$ran = $ar[array_rand($ar, 1)];
echo $ran;
?>
kalam
how to generate float random number. :-
/* echo floatVal(rand(1, 15).'.'.rand(1, 9));
echo "
";
$randomFloat = rand(5, 50) / 10;
//var_dump($randomFloat);
echo $randomFloat;
echo "
";
echo floatVal('0.'.rand(1, 9));
echo "
";
echo rand(0,1000)/1000;
*/
Output :-
how to generate float random number. :-
/* function randomFloat($min = 0, $max = 1) {
return $min + mt_rand() / mt_getrandmax() * ($max - $min);
}
var_dump(randomFloat());
echo "
";
var_dump(randomFloat(2, 20));
echo "
";
echo (randomFloat());
echo "
";
echo (randomFloat(2, 20));
echo "
";
echo mt_getrandmax();
*/
Output :-
how to generate float random number. :-
function GetRandomValue($min, $max)
{
$range = $max-$min;
$num = $min + $range * mt_rand(0, 32767)/32767;
$num = round($num, 4);
return ((float) $num);
}
echo GetRandomValue(5, 50);
Output :-
How to get random text from text file :-
File Name:- mytext.txt
mahtab
habib
i am Software developer
my friend name is Danish
my favorite movies is DDLJ
kabhi
eqacbZ
MsYgh
cSaxyksj
gSnjkckn
dksYdrk
iq.ks
t;iqj
t;iqj
tkya/kj
ifrvyk
cfBaMk
iBkudksV
paMhx<+
convert hindi word to unicode.
function RandomLine($filename) {
$lines = file($filename) ;
return $lines[array_rand($lines)] ;
}
$randomtext = RandomLine("mytext.txt");
echo $randomtext;
?>
Output :-
How to get Random image form directory.
$root = '';
$path = 'images/';
function getImagesFromDir($path) {
$images = array();
if ( $img_dir = @opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[] = $img_file;
}
}
closedir($img_dir);
}
return $images;
}
function getRandomFromArray($ar) {
mt_srand( (double)microtime() * 1000000 );
$num = array_rand($ar);
return $ar[$num];
}
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
$dest1 = imagecreatefrompng($path.$img);
// $dest = imagecreatefromjpeg($path.$img);
/*************************/
//$dest = imagecreatefromjpeg('images/result.jpg');
?>
range() Function.
The range() function creates an array containing a range of elements.
This function returns an array of elements from low to high.
Note: If the low parameter is higher than the high parameter, the range array will be from high to low.
Syntex:
range(low,high,step)
$number = range(0,5);
print_r ($number);
?>
Output:-
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
$number = range(0,50,10);
print_r ($number);
?>
Output:-
Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 )
$letter = range("a","d");
print_r ($letter);
?>
Output:-
Array ( [0] => a [1] => b [2] => c [3] => d )
in_array() Function
The in_array() function searches an array for a specific value.
Syntax :-
in_array(search,array,type)
$name = array("mahtab", "raj", "sara", "kalam");
if (in_array("sara", $name))
{
echo "Match found";
}
else
{
echo "Match not found";
}
?>
$people = array("Peter", "Joe", "Glenn", "Cleveland", 23);
if (in_array("23", $people, TRUE))
{
echo "Match found
";
}
else
{
echo "Match not found
";
}
if (in_array("Glenn",$people, TRUE))
{
echo "Match found
";
}
else
{
echo "Match not found
";
}
if (in_array(23,$people, TRUE))
{
echo "Match found
";
}
else
{
echo "Match not found
";
}
?>
array_merge()
Merge two arrays into one array:
The array_merge() function merges one or more arrays into one array.
$a1=array("mahtab","habib");
$a2=array("kalam","Ashu");
print_r(array_merge($a1,$a2));
?>
Note: If two or more array elements have the same key, the last one overrides the others.
Note: If you assign only one array to the array_merge() function, and the keys are integers, the function returns a new array with integer keys starting at 0 and increases by 1 for each value (See Example 2 below).
Output :-
Array ( [0] => mahtab [1] => habib [2] => kalam [3] => Ashu )
$array1 = array("name" => "mahtab", 10, 15);
$array2 = array("a", "b", "name" => "habib", "color" => "white", 15);
$result = array_merge($array1, $array2);
print_r($result);
?>
Output :-
Array
(
[name] => habib
[0] => 10
[1] => 15
[2] => a
[3] => b
[shape] => white
[4] => 15
)
$array1 = array('10','20','30','100');
$array2 = array("30","40","50","20",'200');
$merg = array_merge($array1,$array2);
print_r($merg);
?>
Output :-
Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 100 [4] => 30 [5] => 40 [6] => 50 [7] => 20 [8] => 200 )
Merge two associative arrays into one array:
NOTE :- if both array contain same key then it override the second value of the array.
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge($a1,$a2));
?>
Output :-
Array ( [a] => red [b] => yellow [c] => blue )
Using only one array parameter with integer keys:
$a=array(3=>"red",4=>"green");
print_r(array_merge($a));
?>
Output :-
Array ( [0] => red [1] => green )
array_combine
array_combine ? Creates an array by using one array for keys and another for its values
array_combine(): Both parameters should have an equal number of elements
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);
print_r($c);
?>
Output :-
Array ( [green] => avocado [red] => apple [yellow] => banana )
$a = array('green', 'red', 'yellow','orange');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);
print_r($c);
?>
Output :-
Warning: array_combine(): Both parameters should have an equal number of elements in D:\xampp\htdocs\test\arraymerge.php on line 4
Difference between array_merge() & array_combine()
array_merge():-array_merge() function Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the value for that key of first array will overwrite by the value of next array. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be added at the last of the arrray.
array_merge merges the elements of one or more than one array such that the value of one array appended at the end of first array. If the arrays have same strings key then the later value overrides the previous value for that key .
$array1= array("name" =>"vikram", 2, 5);
$array2 = array("a", "b", "name" =>; "mohan", "designation" => "developer", 5);
$arrayMergeResult = array_merge($array1, $array2);
print_r($arrayMergeResult);
?>
Output :-
array(
[name] => mohan [0] => 2 [1] => 5 [2] => a [3] => b [destination] => developer [4] => 5
)
array_combine()
array_combine() ? Creates an array by using one array as its keys and another for its values.
$array1 = array('name', 'designation', 'city');
$array2 = array('vikram', 'developer', 'jaipur');
$arrayCombineResult = array_combine($array1, $array2);
print_r($arrayCombineResult);
?>
Output :-
Array ( [name] => vikram [designation] => developer [city] => jaipur )
How to get last word of your String.
$name = "mahtab alam";
$parts = explode(" ", $name);
$lastname = array_pop($parts);
$firstname = implode(" ", $parts);
echo "Lastname: $lastname\n";
echo "Firstname: $firstname\n";
array_filter() function
array_filter() function is predefine function in PHP. this function is used for filter the array elements using callback function.
File Name :
Syntax :- array_filter(array, callbackfunction, flag_value)
here flag_value parameter is optional. Its default value is 0
If the flag_value is set to ARRAY_FILTER_USE_KEY, it send only array keys to the callback function. and if the flag_value is set ARRAY_FILTER_USE_BOTH, then both array keys and values are sent to the callback function.
$myarray = ['sana', 'mahira', 'Mahtab', 'habib', 'Arhamkalam', 'mahi'];
$result_array = array_filter($myarray, function($name){
// it return name whose name is more than 4 characters
if(strlen($name) > 4){
return $name;
}
});
print_r($result_array );
?>
Filter odd even number using array_filter()
this function is used for filter the array elements.
File Name :
function test_even_odd($array)
{
// returns if the input integer is even
if($array%2==0)
return TRUE;
else
return FALSE;
}
$myarray = array(1,3,2,3,4,6,8,9);
print_r(array_filter($myarray ,"test_even_odd"));
?>
Remove elements from array.
File Name :
$mahi = array(1,3,2,3,0,6,8,9,0);
foreach($mahi as $array_item)
{
if($array_item==0)
{
unset($array_item);
}
print_r($array_item);
}
?>
Remove Elements from array
File Name :
<?php
$arr = array(0,10,0,15,0,25);
$remove = array(0);
$result = array_diff($arr, $remove);
echo"<pre>";
print_r($result);
echo"</pre>";
?>
Previous
Next