Home arrow PHP arrow Page 2 - PHP Array Function Tutorial

Common Array String Functions - PHP

PHP array string functions are some of the most important functions you need to know in PHP if you want to become an efficient programmer. In this tutorial, we will look at the most commonly used array string functions, including (but not limited to) count, push, in_array, asort, and pop.

  1. PHP Array Function Tutorial
  2. Common Array String Functions
  3. PHP asort, array_pop, array_merge, and array_search Functions
By: Codex-M
Rating: starstarstarstarstar / 3
May 11, 2011

print this article



1.) count()= This is a function that can be used to count all elements that present in an array. For example:

$nameofgirl= array("Amanda","Jenna","Elizabeth");
echo "The number of girl names in the array is ".count($nameofgirl);

The above result is 3. There is a similar function to count() that produces the same result known as the sizeof() function.

2.) array_push() = this is used when inserting/adding new elements to an existing array. Supposing you will add the following names to the $nameofgirl array variable earlier:

a.) Linda
b.) Jenny
c.) Anna

$nameofgirl= array("Amanda","Jenna","Elizabeth");
//add new girl games to the existing array
array_push($nameofgirl, "Linda", "Jenny","Anna");

This is the output (take note that the new elements added are pushed to the end of the array):

array(6) {
[0]=> string(6) "Amanda"
[1]=> string(5) "Jenna"
[2]=> string(9) "Elizabeth"
[3]=> string(5) "Linda"
[4]=> string(5) "Jenny"
[5]=> string(4) "Anna"

Another way to add elements in the array is by equating the string to be inserted with the array variable:

$arrayvariable[]="String to be inserted to the array"

Illustration: Add the five letters to an existing array of alphabetical letters.

//Read the five letters array by looping on it and then adding it to the existing array
foreach ($fiveletters as $value) {
//dump the updated elements in $existing array

This is the result:

array(8) {
[0]=>string(1) "a"
[1]=>string(1) "b"
[2]=>string(1) "c"
[3]=>string(1) "d"
[4]=>string(1) "e"
[5]=>string(1) "f"
[6]=>string(1) "e"
[7]=>string(1) "f"

3.) in_array() = check if a specific string is present in an array. Example: Supposing you will check if the name “Linda” is present in the $nameofgirl array.

$nameofgirl= array("Amanda","Jenna","Elizabeth");
array_push($nameofgirl, "Linda", "Jenny","Anna");
//check if name "Linda" is present in the array
if (in_array("Linda",$nameofgirl)) {
echo "Yes the name Linda is found on the array";
} else {
echo "Sorry but the name Linda is not on the array";

The above code will return “Yes the name Linda is found in the array”.

4.) array_unique() = removes duplicate values on the array. Example:

Supposing you have an array of colors:

//Supposing you have an array of colors, red is a duplicate

Below is the dump of this array (take note of the index key value of each string element):

array(5) {
[0]=>string(4) "blue"
[1]=>string(3) "red"
[2]=>string(6) "yellow"
[3]=>string(3) "red"
[4]=>string(7) "magenta"

Now remove the duplicate, using array_unique:

//remove the duplicates in the array
//output to browser

This is the dump results of the array_unique function. You will notice that the array element with key index 3 is gone because it is a duplicate.

array(4) {
[0]=>string(4) "blue"
[1]=>string(3) "red"
[2]=>string(6) "yellow"
[4]=>string(7) "magenta"

>>> More PHP Articles          >>> More By Codex-M

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: