PHP Arrays

PHP shuffle() Function Example

PHP Script:


<?php
$sample_array = array("a" => "Samsung", "b" => "Iphone", "c" => "HTC", "d" => "Nokia", "e" => "Huawei");
shuffle($sample_array);
print_r($sample_array);
?>

Output:


Array ( [0] => Huawei [1] => Samsung [2] => HTC [3] => Iphone [4] => Nokia )

Description:

PHP shuffle() function is used to randomly reorder the elements in an array. And the existing keys of an array will be removed and new keys are assigned for the elements in an array.
This function returns TRUE on success, or FALSE on failure.
PHP shuffle() function parameters:
1- Array (Required)

PHP array_search() Function Example

PHP Script:


<?php
$array = array(0 => 'book', 1 => 'cup', 'index'=>'text', 2 => 'spoon',3 => 'fork');
$key = array_search('text', $array);
echo "$key\n";
$key = array_search('cup', $array);
echo "$key\n";
?>

Output:


index
1
Description:
PHP array_search() function is used for searching an array for a given value and returns the corresponding array key if the given value is found.
PHP array_search() function parameters:
1- mixed: (required): The value to be searched.
2- Array (required): The array in which value is searched.
3- Bool(optional), Default False: This Boolean flag when set to true, then this function tires to check the type of the value begin searched and checks if both items references to the same  object.

PHP count() Function Example

PHP Script:


<?php
$lang_arr = array("PHP","JAVASCRIPT","HTML","CSS");
echo count($lang_arr);
$lang_multi_arr = array(
"server_site"=>array("PHP","JSP","ASP"),
"client_site"=>array("JAVASCRIPT","HTML","CSS")
);
echo "<br>";
echo count($lang_multi_arr);
echo "<br>";
echo count($lang_multi_arr,1);
?>

Output:


4
2
8

No comments:

Post a Comment