Joined: Tue Mar 27, 2007 10:55 pm Posts: 2103 Location: Earth Has thanked: 39 time Have thanks: 56 time
With PHP 4.0 comes more than 30 new array-related functions. Some of the more common functions let you determine if something is in a given array or not, count the number of elements in an array, add or delete array elements, and sort array elements.
If you have a large array and all you need to do is find out if a given value exists, you can use in_array() to return true or false. The following will print "Not found in this array" because you're looking for "Albert" in the $namesArray, which does not exist.
You can add elements to any array, either at the end or the beginning of an existing array. You can also use array_merge() to create a new array consisting of the elements of two or more arrays. When merging, each array is tacked on in the order that it is requested. If your arrays have an internal sort order already in place, you'll have to re-sort your new, merged array.
Let's start with adding elements at the end of an existing array, using
array_push():
Code:
<? /* create the original array */ $fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* add to the original array */ array_push($fruitArray, "grape", "pineapple", "tomato");
/* list each element, with its key */ while (list($key,$value) = each($fruitArray)) { echo "$key : $value<br>"; }
?>
This will display: 0 : apple 1 : orange 2 : banana 3 : kiwi 4 : pear 5 : grape 6 : pineapple 7 : tomato
The code is remarkably similar if you need to add some elements to the beginning of an array,. The only difference is the function name: array_unshift() instead of array_push().
Code:
<? /* create the original array */ $fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* add to the original array */ array_unshift($fruitArray, "grape", "pineapple", "tomato");
/* list each element, with its key */ while (list($key,$value) = each($fruitArray)) { echo "$key : $value<br>"; }
?>
This will display: 0 : grape 1 : pineapple 2 : tomato 3 : apple 4 : orange 5 : banana 6 : kiwi 7 : pear
The array_merge() function smashes two or more arrays together.
Code:
<? /* create the first array */ $fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* create the second array */ $vegArray = array("carrot", "green beans", "asparagus", "artichoke", "corn");
/* merge the arrays into one */ $goodfoodArray = array_merge($fruitArray, $vegArray);
/* list each element, with its key */ while (list($key,$value) = each($goodfoodArray)) { echo "$key : $value<br>"; }
?>
This will display: 0 : apple 1 : orange 2 : banana 3 : kiwi 4 : pear 5 : carrot 6 : green beans 7 : asparagus 8 : artichoke 9 : corn
Now that you've added and merged arrays, let's run through the functions for deleting elements from arrays. You can delete one element from the end of an array, using array_pop(). If you use array_shift(), you're deleting an element from the beginning of an array. While you are in fact deleting the element from the array, this element is still available to you as a variable, when you pop it or shift it from the existing array.
Try using array_pop() to delete a value from the end of an array:
/* pop something off the end */ $popped = array_pop($fruitArray);
/* list the contents of the new array, as well as the value you popped off */ while (list($key,$value) = each($fruitArray)) { echo "$key : $value<br>"; }
echo "<br>and finally, in $popped: $popped";
?>
This will display: 0 : apple 1 : orange 2 : banana 3 : kiwi
/* shift something from the beginning */ $shifted = array_shift($fruitArray);
/* list the contents of the new array, as well as the value you shifted off */ while (list($key,$value) = each($fruitArray)) { echo "$key : $value<br>"; }
echo "<br>and finally, in $shifted: $shifted";
?>
This will display: 0 : orange 1 : banana 2 : kiwi 3 : pear
and finally, in $shifted: apple
There are several functions that assist you in sorting array elements, but I'll show you just the basic sort so that you understand the process:
Code:
<? /* create the original array */ $fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* sort the array */ sort($fruitArray);
/* reset it so you can display it properly from beginning to end */
/* list each element, with its key */ while (list($key,$value) = each($fruitArray)) { echo "$key : $value<br>"; }
?>
This will display: 0 : apple 1 : banana 2 : kiwi 3 : orange 4 : pear
Article by:Julie Meloni
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )
foodland
Question subject: Re: Tips for PHP scripts
Posted: Sat Jan 10, 2009 5:45 pm
Joined: Sat Nov 01, 2008 10:28 am Posts: 20 Has thanked: 0 time Have thanks: 0 time
Joined: Wed Jul 22, 2009 11:07 am Posts: 3 Has thanked: 0 time Have thanks: 0 time
Thank you for that useful information,I a learner in PHP trying to building some website which would have some database in backend;i.e Mysql,so can you please help me out.
Also, when dealing with file uploads & security, you should validate the extension - malicious code could be run on your server via file upload.
msi_333
Question subject: Re: Tips for PHP scripts
Posted: Wed Jul 29, 2009 11:28 pm
Joined: Tue Mar 27, 2007 10:55 pm Posts: 2103 Location: Earth Has thanked: 39 time Have thanks: 56 time
well if you have more important tips , please share .
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )
Jayne Carreen
Question subject: Re: Tips for PHP scripts
Posted: Thu Jul 30, 2009 6:06 am
Joined: Mon Jul 27, 2009 7:14 am Posts: 13 Has thanked: 0 time Have thanks: 0 time
Hi, How do I take information from a session and add it to a database? I want to take the persons username of the person submitting a the form and add it to a database.