VisualBuilder
  Home > Php > Tutorials > Arrays in PHP - PHP Tutorial
Tell a friend
Link to us
Total Members
      Members: 84661
     
Sitemap Forum Chat
Home
PHP Tutorial Home
1 . An Introduction to PHP
2 . Getting Started With Variables
3 . Comments in PHP
4 . Operators
5 . The Switch Statement
6 . Logical Operators
7 . Boolean Values
8 . Working with HTML Forms
9 . Programming Loops
10 . Arrays in PHP
11 . String Manipulation
12 . Create your own Functions
13 . Date and Time Functions in PHP
14 . Working With Files In PHP
 
 
PHP Home
PHP Members (3076)
PHP Member Articles ( 9 )
PHP Discussion (11)
PHP Q & A ( 13 )
- PHP Ask Question
- PHP Questions
- PHP Unanswered Questions
PHP Resources
PHP Source Code (463)
PHP Articles (118)
PHP Blogs (30)
PHP Jobs (0)
PHP Components (24)
PHP Books (3)
PHP Websites (29)
PHP News (17)
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.Net Security,Internationalisation And Deployment
ASP.NET Tutorial
C# Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (451 )
Source Code (3275 )
Articles (359 )
Books (372 )
Components (1596 )
News (892 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise

Php Tutorial
 Arrays in PHP
  << Prev: Programming Loops Next: String Manipulation >>

Arrays in PHP

1. What is an Array?


Variable can hold a single value at a time whereas an array is like a special variable,which can hold more than one number,or more than one string,at a time.



2. Setting up an Array in PHP


 


array()


<?


$names = array("Doree","Nemo","Marlin","Jeff");            


$numbers = array("10","20","30","40");


$numStr = array("10","Doree","30","Nemo");


?>



3. Getting at the values stored in Arrays


<?


echo $names[0]; // print Doree


echo $names[1]; // print Nemo


echo $names[2]; // print Marlin


echo $names[3]; // print Jeff


?>
4. Arrays - Using Text as Keys


<?


 


$contactInfo = array( );


$contactInfo["name"] = "Some Name";
$contactInfo["address"] = "Some Address";
$contactInfo["phone"] = "123 456 7890";
$contactInfo["fax"] = "098 765 4321";


 


?>
5. Arrays and For Each


<?


foreach ($contactInfo as $key_name => $key_value)


{
   echo  "Key = " . $key_name . " Value = " . $key_value . "<BR>";
}


?>



6. Sorting Array values


There may be times when you want to sort the values inside of an array. There are many functions in php to sort values in an array. Some of them are;


 


<?


asort($names) – Sort an array and maintain index association


arsort($names) - Sort an array in reverse order and maintain index association


rsort($names) – Sorts an array in reverse order
krsort($names) - Sort an array by key in reverse order


?>



7. Random Keys from an Array


<?


$numbers = array(1 => 1,2 => 3,3 => 5,4 => 7,5 => 9,6 => 11);
$random_key = array_rand($numbers,1);
echo $random_key;


?>



8. The count function


 


<?


$numbers = array(1 => 1,2 => 3,3 => 5,4 => 7,5 => 9,6 => 11);



$array_count = count($numbers);


echo $array_count;


?>


  << Prev: Programming Loops Next: String Manipulation >>
Php Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
 
ADVERTISEMENT
Partner List
Code Project
ASP Alliance
More
 
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising