|
Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index
PHP Script Tips - PHP Built-in Functions for Arrays
By: FYICenter.com
Part:
1
2
3
4
5
6
7
A collection of 19 tips on PHP functions on arrays. Clear answers are provided with tutorial exercises on searching keys and values, sorting arrays, merging two arrays, looping on array elements, expanding and truncating arrays.
Topics included in this collection are:
- How To Get the Total Number of Values in an Array?
- How Do You If a Key Is Defined in an Array?
- How To Find a Specific Value in an Array?
- How To Get All the Keys Out of an Array?
- How To Get All the Values Out of an Array?
- How To Sort an Array by Keys?
- How To Sort an Array by Values?
- How To Join a List of Keys with a List of Values into an Array?
- How To Merge Values of Two Arrays into a Single Array?
- How To Use an Array as a Queue?
- How To Use an Array as a Stack?
- How To Randomly Retrieve a Value from an Array?
- How To Loop through an Array without Using "foreach"?
- How To Create an Array with a Sequence of Integers or Characters?
- How To Pad an Array with the Same Value Multiple Times?
- How To Truncate an Array?
- How To Join Multiple Strings Stored in an Array into a Single String?
- How To Split a String into an Array of Substring?
- How To Get the Minimum or Maximum Value of an Array?
How To Get the Total Number of Values in an Array?
You can get the total number of values in an array by using the count() function.
Here is a PHP example script:
<?php
$array = array("PHP", "Perl", "Java");
print_r("Size 1: ".count($array)."\n");
$array = array();
print_r("Size 2: ".count($array)."\n");
?>
This script will print:
Size 1: 3
Size 2: 0
Note that count() has an alias called sizeof().
How Do You If a Key Is Defined in an Array?
There are two functions can be used to test if a key is defined in an array or not:
- array_key_exists($key, $array) - Returns true if the $key is defined in $array.
- isset($array[$key]) - Returns true if the $key is defined in $array.
Here is a PHP example script:
<?php
$array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java");
print("Is 'One' defined? ".array_key_exists("One", $array)."\n");
print("Is '1' defined? ".array_key_exists("1", $array)."\n");
print("Is 'Two' defined? ".isset($array["Two"])."\n");
print("Is '2' defined? ".isset($array[2])."\n");
?>
This script will print:
Is 'One' defined? 1
Is '1' defined?
Is 'Two' defined? 1
Is '2' defined?
How To Find a Specific Value in an Array?
There are two functions can be used to test if a value is defined in an array or not:
- array_search($value, $array) - Returns the first key of the matching value in the array, if found.
Otherwise, it returns false.
- in_array($value, $array) - Returns true if the $value is defined in $array.
Here is a PHP script on how to use arrary_search():
<?php
$array = array("Perl", "PHP", "Java", "PHP");
print("Search 1: ".array_search("PHP",$array)."\n");
print("Search 2: ".array_search("Perl",$array)."\n");
print("Search 3: ".array_search("C#",$array)."\n");
print("\n");
?>
This script will print:
Search 1: 1
Search 2: 0
Search 3:
How To Get All the Keys Out of an Array?
Function array_keys() returns a new array that contains all the keys of a given array.
Here is a PHP script on how to use array_keys():
<?php
$mixed = array();
$mixed["Zero"] = "PHP";
$mixed[1] = "Perl";
$mixed["Two"] = "Java";
$mixed["3"] = "C+";
$mixed[""] = "Basic";
$mixed[] = "Pascal";
$mixed[] = "FORTRAN";
$keys = array_keys($mixed);
print("Keys of the input array:\n");
print_r($keys);
?>
This script will print:
Keys of the input array:
Array
(
[0] => Zero
[1] => 1
[2] => Two
[3] => 3
[4] =>
[5] => 4
[6] => 5
)
(Continued on next part...)
Part:
1
2
3
4
5
6
7
|