Using The Array_Push () Function
<?php
$vocalists = array ("A", "B", "C");
$musicians = array_push($vocalists, "D", "E", "F"); echo "The total number of musicians are $musicians:", "\n"; foreach ($vocalists as $val) {
echo "$val", "\n";
}
?>Using The Array_Remove() Function To Remove Several Elements From The Beginning And The End Of An Array:
<?php $languages = array( 'French', 'German', 'Russian', 'Chinese'); printf("<p>Original array:\n%s</p>\n", var_export($languages, TRUE)); $num = 2; $removed1 = array_remove($languages, 0, $num); $removed2 = array_remove($languages, count($languages) - $num, $num); printf("<p>Removed (start): %s<br />Removed (end): %s<br /> Remaining: %s</p>\n", var_export($removed1, TRUE), var_export($removed2, TRUE), var_export($languages, TRUE)); ?>
Using_The Array_Shift () Function
<?php
$music = array ("A", "B", "C", "D");
while (count ($music)) {
$somevalue = array_shift ($music);
echo "$somevalue", "<br />";
echo count ($music);
echo "<br />";
}
?>Using The Array_Slice () Function
<?php
$musicgenre = array ("A","B","C","D","E","F","G"); echo "array_slice ($musicgenre, 4, 2):", "<br />"; $Dave = array_slice ($musicgenre, 4, 2);
foreach ($Dave as $valone) {
echo "$valone", "<br />";
}
echo "<br />";
echo "array_slice ($musicgenre, 1, 3):", "<br />"; $Brad = array_slice ($musicgenre, 1, 3);
foreach ($Brad as $valtwo) {
echo "$valtwo", "<br />";
}
echo "<br />";
echo "array_slice ($musicgenre, 3, 3):", "<br />"; $Joe = array_slice ($musicgenre, 3, 3);
foreach ($Joe as $valthree) {
echo "$valthree", "<br />";
} echo "<br />";
echo "array_slice ($musicgenre, -4, 2):", "<br />";
$Rick = array_slice ($musicgenre, -4, 2);
foreach ($Rick as $valfour) {
echo "$valfour", "<br />";
}
echo "<br />";
echo "array_slice ($musicgenre, 1):", "<br />";
$John = array_slice ($musicgenre, 1);
foreach ($John as $valfive) {
echo "$valfive", "<br />";
}
?>Using The Array_Unique () Function
<?php
$oldarray = array ("A", "B", "C", "D", "E", "F");
$newarray = array_unique ($oldarray);
foreach ($newarray as $val) {
echo "$val", "\n";
}
?>Using The Explode() Function
<?phpphp $input_string = "this is a test."; $pieces = explode(",", $input_string); echo "Names in List: <br/>\n"; foreach($pieces as $name) { echo trim($name) . "<br/>\n"; }?>
Using The Implode() Function
<?phpphp $input_array = array("J", "M", "M"); $orig_string = implode(", ", $input_array);?>
Using The Sort () Function
<?php
$emp_names []= "D";
$emp_names []= "B";
$emp_names []= "A";
$emp_names []= "C";
$emp_names []= "E";
sort ($emp_names);
foreach ($emp_names as $val) {
echo "$val", "\n";
}
?>Using Uasort() To Sort A Multdimensional Associative Array By One Of Its Fields
<?php
$products = array( array( name=>"A", price=>4.5 ),
array( name=>"C", price=>5.5 ),
array( name=>"D", price=>2.5 ),
array( name=>"B", price=>2.5 )
);
function priceCmp( $a, $b ){
if ( $a[price] == $b[price] )
return 0;
if ( $a[price] < $b[price] )
return -1;
return 1;
}
uasort( $products, priceCmp );
foreach ( $products as $key => $val )
print "$key: $val[price]<br />\n";
?>Using Uksort() To Sort An Associative Array By The Length Of Its Keys
<?php
$exes = array( x => 1,
xxx => 3,
xx => 2,
xxxxx => 4,
xxxxxx => 5
);
function priceCmp( $a, $b ){
if ( strlen( $a ) == strlen( $b ) )
return 0;
if ( strlen( $a ) < strlen( $b ) )
return -1;
return 1;
}
uksort( $exes, priceCmp );
foreach ( $exes as $key => $val )
print "$key: $val<br />\n";
?>Using Usort() To Sort A Multidimensional Array By One Of Its Fields
<?php
$products = array( array( name=>"A", price=>4.5 ),
array( name=>"C", price=>5.5 ),
array( name=>"D", price=>2.5 ),
array( name=>"B", price=>2.5 )
);function priceCmp( $a, $b ){
if ( $a[price] == $b[price] )
return 0;
if ( $a[price] < $b[price] )
return -1;
return 1;
}
usort( $products, priceCmp );
foreach ( $products as $val )
print "$val[name]: $val[price]<br />\n";
?>Using Variable Variables To Create Associations
<?phpphp $favorite_color = 'blue'; $favorite_weapon = 'gun'; $favorite_drink = 'beer';$favorite_things = array('color', 'weapon', 'drink');foreach ($favorite_things as $thing) { echo '<p>', $thing, ' = ', ${"favorite_{$thing}"}, '</p>'; } ?>
Usort() Sorts An Array Based On Your Own Predefined Criteria.
Its syntax is: void usort(array array, string function_name)<? $vocab = array("S123","A1234", "P12345", "A1234567","T"); function compare_length($str1, $str2) { $length1 = strlen($str1); $length2 = strlen($str2); if ($length1 == $length2) : return 0; elseif ($length1 < $length2) : return -1; else : return 1; endif; } usort($vocab, "compare_length"); while (list ($key, $val) = each ($vocab)) { echo "$val<br />"; } ?>
Validating A Drop-Down Menu With Array_Key_Exists()
<?phpphp$choices = array('eggs' => 'E', 'toast' => 'T', 'coffee' => 'C'); echo "<select name='food'>\n"; foreach ($choices as $key => $choice) { echo "<option value='$key'>$choice</option>\n"; } echo "</select>";if (! array_key_exists($_POST['food'], $choices)) { echo "You must select a valid choice."; } ?>
Working With Unique Values
<?phpphp $countries = array( 'USA' => 'English', 'Spain' => 'Spanish', 'Brasil' => 'Portuguese', 'UK' => 'English', 'France' => 'French', 'Argentina' => 'Spanish'); $languages = array_unique($countries); printf("<pre>%s
\n”, var_export($languages, TRUE)); $languages = array_unique( array_values($countries) );
printf(“
%s
\n”, var_export($languages, TRUE)); $languages = array_values( array_unique($countries) );
printf(“
%s
\n”, var_export($languages, TRUE));
?>
Sorting With Arsort()
<?php $meal = array('breakfast' => 'A', 'lunch' => 'B', 'snack' => 'C', 'dinner' => 'D');print "Before Sorting:\n"; foreach ($meal as $key => $value) { print " \$meal: $key $value\n"; }arsort($meal);print "After Sorting:\n"; foreach ($meal as $key => $value) { print " \$meal: $key $value\n"; } ?>
Sorting With Asort()
<?php $meal = array('breakfast' => 'A', 'lunch' => 'B', 'snack' => 'C', 'dinner' => 'D');print "Before Sorting:\n"; foreach ($meal as $key => $value) { print " \$meal: $key $value\n"; }asort($meal);print "After Sorting:\n"; foreach ($meal as $key => $value) { print " \$meal: $key $value\n"; } ?>
Sorting With Ksort()
<?php $meal = array('breakfast' => 'A', 'lunch' => 'B', 'snack' => 'C', 'dinner' => 'D');print "Before Sorting:\n"; foreach ($meal as $key => $value) { print " \$meal: $key $value\n"; }ksort($meal);print "After Sorting:\n"; foreach ($meal as $key => $value) { print " \$meal: $key $value\n"; } ?>
Sorting With Sort()
<?php $dinner = array('A', 'B', 'C'); $meal = array('breakfast' => 'A', 'lunch' => 'B', 'snack' => 'C', 'dinner' => 'D');print "Before Sorting:\n"; foreach ($dinner as $key => $value) { print " \$dinner: $key $value\n"; } foreach ($meal as $key => $value) { print " \$meal: $key $value\n"; }sort($dinner); sort($meal);print "After Sorting:\n"; foreach ($dinner as $key => $value) { print " \$dinner: $key $value\n"; } foreach ($meal as $key => $value) { print " \$meal: $key $value\n"; } ?>
Sort.Php
<?phpphp$grades = array(42,57,98,100,100,43,78,12); sort($grades); print_r($grades);?>
