WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Arrays and Associative Arrays
Categories : PHP, Arrays Click here to Update Your Picture
Pradeep Mamgain
Date : Jul 16th 2002
Grade : 4 of 5 (graded 5 times)
Viewed : 34937
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Pradeep Mamgain
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

Arrays are one of the most powerful parts of the PHP language.You can initialize
your array in following manner.

<?php
$ar[]="Visual";
$ar[]="Basic";$ar[]="is g8";
print_r($ar);
?>

You can also declare using a list.$ar=array("Visual","Basic","Scripting");

Printing a List with commas
<?php
$ar=array("Janifer","Anna","Hingis");
$ar=implode(",",$ar);
print $ar;
?>

array_splice

array_splice is used to shrink an array.array_splice(array init,int start,int
lenth,aray repl.)

<?php
$ar=array("Janifer","Anna","Hingis");
$ar=array_splice($ar,2); // it will remove first two elements
$ar=implode(",",$ar);
print $ar;
?>


array_unique
array_unique eliminates duplicate entries from your array.

<?php
$str=array_unique($ar);
?>

Returning the Current Element from an Array

Use PHP in-built current function or loop through array.
<?
$current_element=current($ar);
?>

count
=====
function is used to count no of item in an array.
<?php
for ($i=0;$i<count($ar);$i++){
print $ar[$i];
}
?>

foreach
=======
<?
php foreach($ar as $element) {
print $element;
}
?>

Finding Elements in One Array but Not Another
=============================================
Use array_diff() function to find the difference.

<?
php$ar1=array("Green","Red","White");
$ar2=array("Red","Blue","Violet");
$diff=array_diff($ar1,$ar2);
foreach ($diff as $element){
print $element;
print "<br>";
}
?>

Appending an Array to another
=============================
Use Built-in array_merge function.
<?php
$ar1=array("Green","Red","White");
$ar2=array("Red","Blue","Violet");
$add=array_merge($ar1,$ar2);
foreach ($add as $element){print $element;
print "<br>";}
?>

You can also + sign to merge the files.$add=$ar1+$ar2;If you dont want to lose
elements that are same in both arrays, you can use the array_merge_recursive()
function.

Reversing an array
==================
use built-in array_reverse function.
<?php
$ar=array("Green","Red","White");
$ar=array_reverse($ar);
foreach ($ar as $element){
print $element;
print "<br>";
}
?>

ASSICIATIVE ARRAY
=================
Associative array has "String Index Association". Associative array represent
relationship.For example "Sachin is Cricket Star", you can represent it as
<?
$star['Cricket']="Sachin";
?>
Constructing associative array is similar to Normal array but you have to
specify both the value and the key.
<?
$star=array("Cricket"=>"Sachin");
?>

Adding new elements in an associative array
===========================================
<?php
//to add a star
$stars=array("Kapil"=>"Cricket, "Anna"=>"Tenis", "Maradona"=>"Football");
$stars["MJ"]="BasketBall";
?>

Associative Array - Testing key presence
========================================
Use PHP's built-in function isset()
<?php
$stars=array("Kapil" => "Cricket", "Anna" => "Tenis", "Maradona" => "Football");
if (isset($stars["Kapil"])) {
echo "OK ...";
}else{
echo "Not Exist ...";
}
?>

Deleting elements in an associative array
=========================================
Use unset buit-in function for deleting n element , you can pass a single element or
a whole array into it.

<?php
$stars=array("Kapil" => "Cricket", "Anna" => "Tenis", "Maradona" => "Football");
unset($stars[Kapil],$stars[Anna]);
foreach($stars as $nam=>$play){
print $nam;
}
?>

Moving through an Associative Array
===================================
use foreach loop
<?php
foreach ($ar as $key=>$value){
//operations
}
?>
*To merge two arrays you can use array_merge() function or simple use +sign
$ar1+$ar2





Link Submition - Allow your visitors to submit links to the site.
Categories : PHP, Arrays, Filesystem, Beginner Guides
BBCode Formatting String
Categories : PHP, HTML, Regexps, Arrays
create a grid out of <INPUT TYPE=TEXT> then saving to a database. Uses a 'multi-dimension array', but not really as the array is just one big array with the index of "[$i][$j]". Have a look at the code and you'll see what I mean.
Categories : PHP, MySQL, Arrays, Databases
Simple conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
Categories : PHP, Arrays, Date Time, Databases, MySQL
CSS style switcher
Categories : PHP, CSS, HTML and PHP, Arrays, Sessions
dynamic table columns
Categories : PHP, HTML and PHP, Arrays, Databases, MySQL
Array Insertion
Categories : PHP, PHP Classes, Arrays
Dynamic Loading of XML array data into ComboBox and Display XML data using PHP + DOM + Javascript.
Categories : PHP, Java Script, DOM XML, XML, Arrays
How to pass an array from one PHP Script to another via an HTML form
Categories : PHP, HTML and PHP, Arrays
How to load a query result into a PHP Array
Categories : PHP, Databases, Arrays, MySQL
array -- Create an array
Categories : PHP, PHP Functions, Arrays
How to get the source of a site into an array.
Categories : Arrays, HTML, PHP
PHPDRAW, the php wannabe Photoshop ;-)
Categories : PHP, PHP Classes, GD image library, Arrays
PHP Script to find url links in a page
Categories : PHP, URLs, Regexps, Arrays
Simple script to passing persistent and growing array between recalls of one page (manipulate little stack).
Categories : Arrays, Global Variables, PHP, HTML and PHP, Variables