Udjuni.com

udjuni.com search:




Array related PHP Functions


The PHP language has a lot of functions that are used to populate, retrieve and manipulate array variables. Bellow are some of those functions, we will be using some of them as we continue to learn.


Functions for populating arrays


array_push () This function s used to add one or more elements at the end of the array. The general format for using array_push is:
array_push($array_name, “element1”, “$element2”); 

array_unshift () This adds one or more elements at the beginning of an existing array. The general format for using array_unshift is:
array_unshift($array_name, “element1”, “$element2”); 


Functions for retrieving array values


each() and list() - These functions usually appear together in a PHP statement. They are used to go through an array, returning its keys and values. For example the following code snippet could be used to return keys and values of an associative array.
while ( list ($namekey, $value) = each ($teacher)){
echo “$namekey has the value : $value”;
}

foreach() This function also goes through an array and may be used to print array values or assign the values to a scalar variable. The general format for using this function is:
foreach ($teacher as $t ){
echo “$c”;
}

This statement goes through the array named $teacher and display every value in that array in a new line.


Functions for modifying array values


Shuffle () - As the name implies, this function randomizes the elements of an existing array. The general format of such a function is as follows:
shuffle ($teacher); 
where $teacher is the name of an existing array.

array_merge () - Also as the name implies, merges or combine the values of two arrays into one. The general format for using this function:
 $all_employees = array_merge ($teacher, $employee); 
where $teacher and $employee are the two arrays being merged into one new array named $all_employees.

array_shift() This function removes and return the first element of an existing array. The general format foe the function is:
$first_element = array_shift ($teacher); 


reset () As the name implies, this function points back to the beginning of an array. The format for using the function:
reset ($teacher); 



array_pop Removes and return the last element of an existing array. The general format for using this function is as follows:
last_array_element = array_pop ($teacher); 
where $teacher is the name of an existing array.

There may be other array functions that we did not cover here, you can find some of these functions on the at : PHP website


RELATED ARTICLES:


Functions Notation


Functions in PHP
Just like we have learned in mathematics, the basic way to think of and understand functions is to think of it as a machine, you feed it with raw materials and the machine goes through processing these materials into finished and possibly usable product. An easy way to look at functions is to consid

MySQL String functions
MySQL relational Database system has a lot of built-in functions, some of which are used to retrieve and manipulate data, while others are simply used for server administrative purposes. In this posting we will explore how String functions are used in MySQL. String length function

PHP Flow control functions


PHP String functions
So far we have only displayed strings directly to the browser without changing the format. We have successfully done that using the PHP echo () command. In addition to the echo () command, PHP also has two other print functions that allow users to format the output. The formatting may come in the fo

Trigonometry functions
Trigonometry is a branch of mathematics that deals with triangles. It is more like geometry, but it deals particularly with right angle triangles. The concept of trigonometry deals with the relationship between angles and sides of the triangle. To accurately descri

PHP Flow control functions
Most scripts we have looked at so far are linear and flow only in one direction. For example we have looked at scripts that would take two numbers add them together and display the answer. In real world things hardly work that way. In most cases you are faced with different possibilities and

php functions


Oracle Procedures, Functions and looping constructs
Procedures and functions in PL/SQL are types of blocks that are stored in the database in a compiled form. This is among the major differences between ordinary queries and procedures and functions (otherwise known as subprograms). Creating a function or procedure requires the u

Arrays in PHP
In the world of programming, you normally use two kinds of variables, one is what is called scalar variables which can only store one value at any given time. Another kind of variable is what is called arrays which are indexed storage location that are able to keep multiple values. By

Did not find it? Try udjuni.com search:







© 2009 UdjunI LLC. All rights reserved | Privacy policy | Tutorial Index | RSS Feed