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)); ?>
Continue reading…

Enjoyed this post? Share it!

 
 

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"; } ?>
Continue reading…

Enjoyed this post? Share it!

 
 

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 […]
Continue reading…

Enjoyed this post? Share it!

 
 

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 ) […]
Continue reading…

Enjoyed this post? Share it!

 
 

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)); ?>
Continue reading…

Enjoyed this post? Share it!

 
 

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 […]
Continue reading…

Enjoyed this post? Share it!

 
 

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."; } ?>
Continue reading…

Enjoyed this post? Share it!

 
 

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 />"; } […]
Continue reading…

Enjoyed this post? Share it!

 
 

Using Array_Merge() And The + Operator With Associative Arrays As Well

<?phpphp function array_display($array, $pre=FALSE) { $tag = $pre ? ‘pre’ : ‘p’; printf("<%s>%s</%s>\n", $tag, var_export($array, TRUE), $tag); } $dogs1 = array(’A’ => ‘C’, ‘B’ => ‘C’, ‘D’ => ‘C’); $dogs2 = array(’a’ => ‘a’, ‘b’ => ‘b’);   array_display(array_merge($dogs1, $dogs2), TRUE); array_display($dogs1 + $dogs2, TRUE); ?>
Continue reading…

Enjoyed this post? Share it!