WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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





columned txt file to array()?
Categories : Arrays, Strings, Regexps, PHP
Print out array key => value in colored HTML
Categories : PHP, Arrays, HTML and PHP
Simple conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
Categories : PHP, Arrays, Date Time, Databases, MySQL
Link Submition - Allow your visitors to submit links to the site.
Categories : PHP, Arrays, Filesystem, Beginner Guides
Looping through two arrays
Categories : PHP, Databases, Arrays
The meaning of your name
Categories : PHP, Arrays, Misc
Form Submission Using Array's
Categories : PHP, HTML and PHP, Beginner Guides, Arrays
PHPDRAW, the php wannabe Photoshop ;-)
Categories : PHP, PHP Classes, GD image library, Arrays
quick sort for associative arrays
Categories : Algorithms, Arrays, PHP
Stepping through a numbered array with each() and list()
Categories : PHP, Arrays
HTML_Graphs uses PHP to provide a consistent interface for creating HTML based charts. The user of the class sets up arrays that are passed to html_graph() which then takes care of all the messy HTML layout.
Categories : Graphics, Arrays, PHP, PHP Classes, Charts and Graphs
PHP Script to find url links in a page
Categories : PHP, URLs, Regexps, Arrays
Tag content retrieval from websites with preg_match
Categories : PHP, Regexps, Arrays, HTML and PHP
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
Parsing html tags with php. Get an array from this function
Categories : PHP, HTML and PHP, Arrays, Tag Extractors