Total members 11893 |It is currently Tue Nov 05, 2024 7:38 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Array-related functions

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.

Code:
<? $namesArray = array("Joe""Jane""Bob""Mary""Paul""Eddie""John");
$lookingFor "Albert";

if (
in_array($lookingFor$namesArray)) {
echo 
"You've found it!";
} else {
echo 
"Not found in this array!";
}
?>


If you change the value of $lookingFor to "Mary", you will receive the "You've found it!" message, since "Mary" is part of the $namesArray.

If you want to count the number of elements in an array, you can use the handy count() function:
Code:

<? $namesArray = array("Joe""Jane""Bob""Mary""Paul""Eddie""John");
$count count($namesArray); ?>


The value of $count is 7.

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

<?
/* create an array */
$fruitArray = array("apple""orange""banana""kiwi""pear");

/* 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

and finally, in $popped: pear

Next, delete an element from the end of an array:

Code:
<?
/* create an array */
$fruitArray = array("apple""orange""banana""kiwi""pear");

/* 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:
Code:
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:
Code:
0 : apple
1 : banana
2 : kiwi
3 : orange
4 : pear




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

Thanks for the tips, I would like to know the shortest way to code PHP. :D


Author:
Newbie
User avatar Posts: 6
Have thanks: 0 time

thanks for your wonderful tips :)


Author:
Newbie
User avatar Posts: 13
Have thanks: 0 time

thank you for added php script code .

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

Thank you for that useful information,I a learner in PHP trying to building some website which would have some database in back-end;i.e MySQL,so can you please help me out.


Last edited by arbaba on Sun Aug 23, 2009 11:46 pm, edited 3 times in total.

Author:
Newbie
User avatar Posts: 3
Have thanks: 0 time

Try this for upload.php:

Code:
$new_dir 'uploads/';
$this_file_name strtolower($_FILES['userfile']['name']);
copy("$userfile"$new_dir $this_file_name); 

Also, when dealing with file uploads & security, you should validate the extension - malicious code could be run on your server via file upload.


Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time

well if you have more important tips , please share .

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 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.


Author:
Newbie
User avatar Posts: 9
Have thanks: 0 time

Jayne Carreen wrote:
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.


use session variable .

Code:

    
if(isset($_SESSION['username'])) 
{
   
// code here to take value from $_SESSION['username'] 
}
 else 
 {
  echo 
" User Not Logged In " 
}
       
 


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

I'm new to PHP. Thanks for these tips, gonna try some of them out now.


Author:
Newbie
User avatar Posts: 3
Have thanks: 0 time
Post new topic Reply to topic  [ 10 posts ] 

  Related Posts  to : PHP Array Functions
 What are Inline functions?     -  
 Functions and References     -  
 What are Virtual Functions?     -  
 Calling Functions Dynamically     -  
 using of scanf and printf functions     -  
 Lets Learn C++----->(Lesson 5) Functions     -  
 program to run shell command line functions     -  
 character running automatically / jump /duck/ functions     -  
 small-minimal shell for Linux- functions for builtin command     -  



Topic Tags

PHP Arrays






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com