Array important functions

Array important functions - Hallo sahabat Tutorials, Pada Artikel yang anda baca kali ini dengan judul Array important functions , kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : Array important functions
link : Array important functions

Baca juga


Array important functions

 Array    important   functions  :

(  (1)    sizeof($arr) :
This function returns the number of elements in an array.
Use this function to find out how many elements an array contains; this information is most commonly used to initialize a loop counter when processing the array.
Example:
<?php

$data = array("red", "green", "blue");

echo "Array has " . sizeof($data) . " elements";

?>
Output:
Array has 3 elements

(2)array_values($arr)

This function accepts a PHP array and returns a new array containing only its values (not its keys). Its counterpart is the array_keys() function.

Use this function to retrieve all the values from an associative array.
write code  array_values.php file:
<?php

$data = array("hero" => "Holmes", "villain" => "Moriarty");
print_r(array_values($data));

?>

Output:
Array ( [0] => Holmes [1] => Moriarty ) 

(3)This function accepts a PHP array and returns a new array containing only its keys (not its values). Its counterpart is the  array_values()   function.
Use this function to retrieve all the keys from an associative array.
write    code     for      array_keys.php    file :
<?php
$data = array("hero" => "Holmes", "villain" => "Moriarty");
print_r(array_keys($data));
?>
Output:
Array ( [0] => hero [1] => villain )
(4)array_pop($arr)  :
This function removes an element from the end of an array.
<?php
$data = array("Donald", "Jim", "Tom");
array_pop($data);
print_r($data);
?>
Output:
Array ( [0] => Donald [1] => Jim )
(5)array_push($arr, $val)
This function adds an element to the end of an array.
write code   array_push.php file:
<?php
$data = array("Donald", "Jim", "Tom");
array_push($data, "Harry");
print_r($data);
?>
output:
Array ( [0] => Donald [1] => Jim [2] => Tom [3] => Harry )

(6)array_shift($arr)
This function removes an element from the beginning of an array.

write code  for  array_shift.php file:
<?php
$data = array("Donald", "Jim", "Tom");
array_shift($data);
print_r($data);
?>
Output:
Array ( [0] => Jim [1] => Tom )
(7)each($arr)
This function is most often used to iteratively traverse an array. Each time each() is called, it returns the current key-value pair and moves the array cursor forward one element. This makes it most suitable for use in a loop.
<?php

$data = array("hero" => "Holmes", "villain" => "Moriarty");
while (list($key, $value) = each($data)) {
echo "$key: $value \n";
}
?>

Output :
hero: Holmes villain: Moriarty

(8) sort($arr)
This function sorts the elements of an array in ascending order. String values will be arranged in ascending alphabetical order.
Note: Other sorting functions include asort(), arsort(), ksort(), krsort() and rsort().
write   code   for  sort.php file:
<?php
$data = array("g", "t", "a", "s");
sort($data);
print_r($data);
?>
output:
Array ( [0] => a [1] => g [2] => s [3] => t )

(9) array_search($search, $arr) :
This function searches the values in an array for a match to the search term, and returns the corresponding key if found. If more than one match exists, the key of the first matching value is returned.
Use this function to scan a set of index-value pairs for matches, and return the matching index.
write    code     array_search.php file:
<?php
$data = array("blue" => "#0000cc", "black" => "#000000", "green" => "#00ff00");
echo "Found " . array_search("#0000cc", $data);
?>
Output:
Found blue

(10)array_slice($arr, $offset, $length) :

This function is useful to extract a subset of the elements of an array, as another array. Extracting begins from array offset $offset and continues until the array slice is $length elements long.

Use this function to break a larger array into smaller ones—for example, when segmenting an array by size ("chunking") or type of data.

Code:
<?php
$data = array("vanilla", "strawberry", "mango", "peaches");
print_r(array_slice($data, 1, 2));
?>

Output:
Array
(
[0] => strawberry
[1] => mango
)
(11 ) array_unique($data)  :

This function strips an array of duplicate values.

Use this function when you need to remove non-unique elements from an array—for example, when creating an array to hold values for a table's primary key.

Code:
<?php
$data =array(1,1,4,6,7,4);
print_r(array_unique($data));
?>

Output:
Array
(
[0] => 1
[3] => 6
[4] => 7
[5] => 4
)
(12)array_walk($arr, $func):

This function "walks" through an array, applying a user-defined function to every element. It returns the changed array.

Use this function if you need to perform custom processing on every element of an array—for example, reducing a number series by 10%.

Code:
<?php
function reduceBy10(&$val, $key) {
$val -= $val * 0.1;
}

$data = array(10,20,30,40);
array_walk($data, 'reduceBy10');
print_r($data);
?>

Output:
Array
(
[0] => 9
[1] => 18
[2] => 27
[3] => 36
)


(13list() function:
<?php

$array = array('one', 'two', 'three');
list($first, $second, $third) = $array;
echo $first;


?>
output:
one


Demikianlah Artikel Array important functions

Sekianlah artikel Array important functions kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel Array important functions dengan alamat link https://othereffect.blogspot.com/2016/02/array-important-functions.html

0 Response to "Array important functions "

Post a Comment