Search:

Categories



Remove duplicate elements from array

<?php
$input = array(“a”=>”apple”, “pear”, “b”=>”apple”, “orange”, “avocado”, “banana”);
print_r($input);
$result = array_unique($input);
print_r($result);
?>

Share

a MySQL search function

if (!function_exists(‘mysql_search’)) {
 
   function mysql_search($table, $columns, $query = ”, $options = Array()) {
 
      if (empty($query)) { return Array(); }
 
      $sql_query = Array();
 
      $options[‘columns’] = isset($options[‘columns’])?$options[‘columns’]:’*’;
      $options[‘method’] = isset($options[‘method’])?$options[‘method’]:’OR’;
      $options[‘extra_sql’] = isset($options[‘extra_sql’])?$options[‘extra_sql’]:”;
 
      $query = ereg_replace(‘[[:<:]](and|or|the)[[:>:]]’, ”, $query);
      $query = ereg_replace(‘ +’, ‘ ‘, trim(stripslashes($query)));
 
      $pattern = ‘/([[:alpha:]:]+)([[:alpha:] ]+)[[:alpha:]]?+[ ]?/i’;
 
      $regs = Array();
 
      preg_match_all($pattern, $query, $regs);
 
      $query = $regs[0];
 
      while (list($key, $value) = @each($query)) {
 
         $column = $columns;
         $keywords = urldecode($value);
 
         if (strpos($value, ‘:’)) {
 
            $column = substr($value, 0, strpos($value, ‘:’));
            $keywords = trim(substr($keywords, strpos($keywords, ‘:’) + 1));
            $keywords = ereg_replace(”’, ”, $keywords);
 
         } else { $keywords = ereg_replace(‘ +’, ‘|’, $keywords); }
 
         $column_list = explode(‘ ‘, $column);
 
         $sql = Array();
 
         for ($i = 0; $i < count($column_list); $i++) { $sql[] = ” . $column_list[$i] . ‘ REGEXP "’ . $keywords . ‘"’; }
 
         $query[$key] = Array(‘orignal’=>$value, ‘sql’=>implode(‘ ‘ . $options[‘method’] . ‘ ‘, $sql));
 
         $sql_query = array_merge($sql_query, $sql);
         $sql_query = implode(‘ ‘ . $options[‘method’] . ‘ ‘, $sql_query);
 
      }
 
      $results = mysql_fetch_results(mysql_query(‘SELECT ‘ . $options[‘columns’] . ‘ FROM ‘ . $table . ‘ WHERE ‘ . $sql_query . ‘ ‘ . $options[‘extra_sql’]));
 
      return $results;
 
   }
 
}

Share

Random Image

<?php    
$images=array(‘a.jpg’,’b.jpg’,’c.jpg’,’d.jpg’);    
$image=$images[array_rand($images)];   
echo ‘<img src=”path/to/’.$image.'” />’;
?>

Share

Add to an array

Add to an array using array_push

<?php
   $search = array(“google”,”yahoo”,”live”,”google”,”yahoo”,”google”);
   print_r($search);
   print “<br>”;
   //lets add a couple of values and print them out again
   array_push($search,”google”,”live”);
   print_r($search);
?>

The result will be

Array ( [0] => google [1] => yahoo [2] => live [3] => google [4] => yahoo [5] => google )
Array ( [0] => google [1] => yahoo [2] => live [3] => google [4] => yahoo [5] => google [6] => google [7] => live )

Share

Search for an item in an array

Search for an item in an array using in_array.

<?php
$items = array(“php”, “asp”, “javascript”);

if (in_array(“php”, $items))
{
echo “found php in the array”;
}
else
{
echo “cannot find the search term”;
}
?>

Share

Random image example

This example uses an array to store images and then produces a random number and display the image on the screen

<?php
//an array of 4 images in the same direcory as the script
$images = array(“a1.gif”,”a2.gif”,”a3.gif”,”a4.gif”);
//generate a random number
srand((double)microtime()*1000000);
$randomno = (rand()%4);
//display image
echo (“<img src=\”$images[$randomno]\”>”);
?>

Share