Php Programming Code Examples
Php > Arrays Code Examples
Arrays and Associative Arrays
Arrays and Associative Arrays
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