Home arrow PHP arrow PHP Array Function Tutorial

PHP Array Function Tutorial

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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Quick Guide to Array Basics

PHP arrays are a simple way of storing numerous bits of information (such as strings) in a simple PHP variable, known as an “array variable”; you can retrieve this information by using its key index.

On the other hand, “strings” are a finite series of symbols from a set such as an alphabet. In PHP, you can designate a string with single or double quotes:

<?php
$string1= 'This is a string created by single quote';
$string2= "This is a string created by double quotes";
?>

Most of the information you gather in a PHP web form is in string data format. Storing strings in an array allows your code to use only a single PHP variable to store a lot of information. Supposing you would like to define an array variable that consist of the planets in the solar system, you would do so in the following manner:

<?php
//create an array variable $planets
$planets=array(); 
//store strings in the $planet array variable, which is essentially the name of the planets
$planets=array("Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune");
//dump all string information stored in the array variable to the browser
var_dump($planets);
?>

This will be the output:

array(8) {
[0]=>string(7) "Mercury"
[1]=>string(5) "Venus"
[2]=>string(5) "Earth"
[3]=>string(4) "Mars"
[4]=>string(7) "Jupiter"
[5]=>string(6) "Saturn"
[6]=>string(6) "Uranus"
[7]=>string(7) "Neptune"
}

The numbers [0] to [7] are the key index values corresponding to each piece of string information stored in the array. You can retrieve string information by calling its key. Supposing you will need to retrieve the planet “Jupiter”, the index key is 4:

<?php
var_dump($planets[4]);
?>
<?php
var_dump($planets[4]);
?>

There are also “associative arrays”, which are useful if you do not use a sequential numerical key index for your string elements. For example:

<?php
//Example1. Associative array of a person and its favorite pet
$favoritepet=array("Mary"=>"Dog","John" =>"Snakes","Paul"=>"Centipede","Peter"=>"Cat");
var_dump($favoritepet);
//Example2. Associative array of a person and its weight in kilos
$weight=array("Mary"=>50,"John" =>60,"Paul"=>80,"Peter"=>71);
var_dump($weight);
?>

Below is the output of the above associative array:

Example1:

array(4) {
["Mary"]=>string(3) "Dog"
["John"]=>string(6) "Snakes"
["Paul"]=>string(9) "Centipede"
["Peter"]=>string(3) "Cat"
}

Example2:

array(4) {
["Mary"]=>int(50)
["John"]=>int(60)
["Paul"]=>int(80)
["Peter"]=>int(71)
}



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: