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
PHP Web Logs (BLogs)
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
Submit Site
Forex Trading Online forex trading platform

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 4 times)
Viewed : 16052
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





Tag content retrieval from websites with preg_match
Categories : PHP, Regexps, Arrays, HTML and PHP
Array values from javascript to php
Categories : PHP, Java Script, Arrays
clearing variables in php3
Categories : Variables, Arrays, PHP
Weighted Random - Random Scripts usually chose one out of each item, and each item have an equal chance to be chosen. But what if you want an item to be chosed more frequently than other?
Categories : PHP, Math., Arrays
Display list of files within current and subdirectories (recursively) showing each file as an anchored link and each directory as a category header.
Categories : Filesystem, Directories, Arrays, PHP
Dump the contents of a PHP variable in html format with a recursive list of subfolders and files from a given root directory.
Categories : PHP, Directories, Variables, Arrays
Array Insertion
Categories : PHP, PHP Classes, Arrays
PHP Script to find url links in a page
Categories : PHP, URLs, Regexps, Arrays
Check for functional file links (broken Files)
Categories : PHP, Data Validation, FTP, Regexps, Arrays
This gets the http response headers for a given url and returns them in an assoc array. i.e. to test if a url exists: $array = get_http_headers($url); if($array[result]=200) { }
Categories : HTTP, Arrays, PHP
Beginners Array Functions
Categories : PHP, Beginner Guides, 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
dynamic table columns
Categories : PHP, HTML and PHP, Arrays, Databases, MySQL
A recursive function to traverse a multi-dimensional array where the dimensions are not known
Categories : Arrays, PHP, Algorithms