Blog

eXorithm – Execute Algorithm: Discuss Algorithm sort_multi_array

sort_multi_array     version 0.1     Sort a two-dimensional array by one (or more) of the elements in the nested arrays. Accepts a variable number of arguments.Subject: Usage Example for sort_multi_array would be useful

optionzz posted: Jan 2, 2014 08:02 pm [reply]

I have a TOUGH time wrapping my head around the strange php array design. All I need to do is use a matrix with numeric indexes for each dimension. Somehow, EVERY example uses names for the "columns" in the second index. If I want that, I’ll use SQL.

Sorry, complaining won’t help. I’d love to see how this would work for a normal 2 dim array like
0 1 2 3
0 (0,0) (0,1) (0,2) (0,3)

2

4 (4,0) (4,1) (4,2) (4,4)

I want to sort all 5 rows based on any of the column NUMBERS (there are no names).

I’m pretty new to PHP but not to programming. Perhaps someone could help break the logjam in my head that REFUSES to understand arrays inside arrays with names instead of numbers, but most importantly, how to make the sort work!
Thanks.

Subject: Usage Example for sort_multi_array would be useful

optionzz posted: Jan 2, 2014 07:58 pm [reply]

I have a TOUGH time wrapping my head around the strange php array design. All I need to do is use a matrix with numeric indexes for each dimension. Somehow, EVERY example uses names for the "columns" in the second index. If I want that, I’ll use SQL.

Sorry, complaining won’t help. I’d love to see how this would work for a normal 2 dim array like
0 1 2 3
0 0,0 0,1 0,2 0,3
1 1,0 1,2…


New Comment

eXorithm – Execute Algorithm: View / Run Algorithm plot_function

function plot_function ($function, $variable, $range_start, $range_end, $width, $height, $pixels_per_plot, $color, $show_grid
{
  // fix the pixels/plot variable
  $pixels_per_plot = floor$pixels_per_plot);
  if ($pixels_per_plot<=0) $pixels_per_plot = 1;
  
  // calculate all our points
  $xy = array();
  for ($ii=0; $ii<($width$pixels_per_plot); $ii$ii$pixels_per_plot) {
    if ($ii>=$width) {
      $x = $range_start + ($range_end - $range_start);
    } else {
      $x = $range_start + $ii$width * ($range_end - $range_start);
    }
    $y = evaluate_for_x$function, $x);
    if (!is_nan$y)) {
      $xy[]=array$x$y);
    }
  }
  
  // find the min and max values of x
  for ($ii=0; $iicount$xy); $ii++) {
    if (is_finite$xy$ii][1])) {
      if ((isset$miny)) && (isset$maxy)))  {
        if ($xy$ii][1]<$miny) $miny = $xy$ii][1];
        if ($xy$ii][1]>$maxy) $maxy = $xy$ii][1];
      } else {
        $maxy = $xy$ii][1];
        $miny = $xy$ii][1];
      }
    }
  }
  if (!isset$maxy)) $maxy = 1;
  if (!isset$miny)) $miny = -1;
  
  // if height is not given, compute it automatically
  if ($height<=0) {
    $height = ceil(($maxy$miny)/($range_end$range_start)*$width);
    if ($height>2000) $height = 2000;
  }
  
  // create blank image
  $image = image_create_alpha$width, $height+1);
  
  // create the colors
  $r  = hexdecsubstr$color, 0, 2));
  $g  = hexdecsubstr$color, 2, 2));
  $b  = hexdecsubstr$color, 4, 2));
  $color = imagecolorallocate$image, $r, $g, $b);
  
  // determine the view port for the y axis (where on the y axis should we focus?)
  $bottom_y = ($maxy$miny)/2 - (($height$width)*($range_end$range_start)/2);
  $top_y = $bottom_y + (($height$width)*($range_end$range_start));
  
  // numbers to shift by so that the graph is focused on the right spot
  $shift_y = $bottom_y*-1;
  $shift_x = $range_start*-1;
  // factor to translate the plotted points to pixel locations
  $mult = $width/($range_end$range_start);
  
  // draw the graph lines
  if ($show_grid) {
    $graph_color = imagecolorallocate$image, 200, 200, 200);
    $minor_color = imagecolorallocate$image, 230, 230, 230);
  
    $line_every = 50/$mult; // lines approximately every 50 pixels
    $places = floorlog$line_every,10))*-1;
    $line_every = round$line_every/5, $places)*5; // try and make the graph lines appear at a nice divisible by 5 number
    if ($line_every==0) $line_every=1; // this shouldn't happen, but just in case don't want to divide by 0
    
    // vertical lines
    $vert_line = floor$range_start$line_every) * $line_every
    while ($vert_line<=$range_end) {
      imageline$image, round(($vert_line$shift_x)*$mult), 0, round(($vert_line$shift_x)*$mult), $height, $minor_color);
      imagestring$image, 1, round(($vert_line$shift_x)*$mult)+1, $height-10, "$vert_line", $graph_color);
      $vert_line = $vert_line$line_every
    }
  
    // horizontal lines
    $horiz_line = floor$bottom_y$line_every) * $line_every
    while ($horiz_line<=$top_y) {
      imageline$image, 0, $heightround(($horiz_line$shift_y)*$mult), $width, $heightround(($horiz_line$shift_y)*$mult), $minor_color);
      imagestring$image, 1, 5, $heightround(($horiz_line$shift_y)*$mult), "$horiz_line", $graph_color);
      $horiz_line = $horiz_line$line_every
    }
    
    // axis lines
    imageline$image, round$shift_x$mult), 0, round$shift_x$mult), $height, $graph_color);
    imageline$image, 0, $heightround$shift_y$mult), $width, $heightround$shift_y$mult), $graph_color);
  }
  
  // draw the line
  for ($ii=0; $iicount$xy)-1; $ii++) {
    
    // infinite number?
    if (is_infinite$xy$ii][1])) {
      if ($xy$ii][1]<0) $xy$ii][1]=$bottom_y-100000;
      else $xy$ii][1]=$top_y+100000;
    }
    
    // translate the plotted values to locations in the image
    $x1 = round(($xy$ii][0]+$shift_x)*$mult);
    $y1 = $heightround(($xy$ii][1]+$shift_y)*$mult);
    $x2 = round(($xy$ii+1][0]+$shift_x)*$mult);
    $y2 = $heightround(($xy$ii+1][1]+$shift_y)*$mult);
    
    // draw the line
    imageline$image, $x1, $y1, $x2, $y2, $color);
  }
  
  return $image
} 

eXorithm – Execute Algorithm: View / Run Algorithm edgify

function edgify ($image$threshold
{
  // from http://community.invisionpower.com/topic/150046-php-functions/
  
  $height = imagesy$image);
  $width = imagesx$image);
  $new_image = imagecreatetruecolor$width$height);
  $white = imagecolorallocate$new_image, 255, 255, 255);
  $black = imagecolorallocate$new_image, 0, 0, 0);
  
  $image = convert2grayscale$image);
  
  // add edges using sobel technique
  for ($x=0; $x$width$x++) {
    for ($y=0; $y$height$y++) {
      $x2 = $x+1;
      $y2 = $y+1;
      if ($x2>=$width$x2 = $x
      if ($y2>=$height$y2 = $y;    
      $p1 = imagecolorat$image$x$y2) & 0xFF;
      $p2 = imagecolorat$image$x2$y2) & 0xFF;
      $p3 = imagecolorat$image$x$y) & 0xFF;
      $p4 = imagecolorat$image$x2$y) & 0xFF;
      $h = abs$p1 - $p4);
      $k = abs$p2 - $p3);
      $g = $h + $k
      if ($g > $threshold){
        imagesetpixel$new_image$x$y$black);
      } else {
        imagesetpixel$new_image$x$y$white);
      }
    }
  }
  
  return $new_image

eXorithm – Execute Algorithm: View / Run Algorithm star_polygon

function star_polygon ($radius$points$skip$color
{
  // blank image
  $image = image_create_alpha$radius*2+4, $radius*2+4);
  
  // create the color
  $r  = hexdecsubstr$color, 0, 2));
  $g  = hexdecsubstr$color, 2, 2));
  $b  = hexdecsubstr$color, 4, 2));
  $color = imagecolorallocate$image$r$g$b);
  
  // The fudge factor is only used to rotate the star so its points line
  // up at the bottom.
  if (($points%2)==0) {
    if (($points/2%2)==0) {
      $fudge = pi()*1/$points
    } else {
      $fudge = 0;
    }
  } else {
    if ((($points-1)/2%2)==0) {
      $fudge = pi()*1.5/$points
    } else {
      $fudge = pi()*0.5/$points
    }
  }
  
  $xy = array();
  
  // for the number of points...
  for ($i=0; $i$points$i++) {
    // compute a point on the perimeter $i/$points from the beginning
    $x = round$radiuscos(2*pi()*$i$points$fudge)+$radius+2);
    $y = round$radiussin(2*pi()*$i$points$fudge)+$radius+2);
    $xy[]=array$x$y);
  }
  
  for ($from=0; $fromcount$xy); $from++) {
    
    // where to draw the line to
    $to = ($from$skip+1) % (count$xy));
    
    // draw the line
    imageline$image$xy$from][0], $xy$from][1], $xy$to][0], $xy$to][1], $color);
  }
  
  return $image

Weather Forecast

<?php

/**
 * weather_forecast
 *
 * Display the weather for a location. Look up using the Google weather API.
 *
 * @version 1.4
 * @author Contributors at eXorithm
 * @link /algorithm/view/weather_forecast Listing at eXorithm
 * @link /algorithm/history/weather_forecast History at eXorithm
 * @license /home/show/license
 *
 * @param mixed $location The location to get the weather for. Can be a city, zip code, or postal code.
 * @param mixed $units The units to get the temperature in.
 * @param string $header_color (hex color code) Color of the header
 * @param string $day_header_color (hex color code) Color of the header for each of the days
 * @param string $day_color (hex color code) Color of the cells containing the forecasts
 * @return string HTML
 */
function weather_forecast($location='Sydney, Australia',$units='c',$header_color='f0f0f0',$day_header_color='d0ffd0',$day_color='f0f0f0')
{
	$weather = file_get_contents('http://www.google.com/ig/api?weather='.urlencode($location));
	
	$xml = simplexml_load_string($weather);
	
	if (!isset($xml->weather->forecast_conditions)) {
		throw new Exception('Data could not be retreived for location '.$location);
	}
	
	$location = $xml->weather->forecast_information->city['data'];
						                                          
	// four day outlook
	for ($i = 0; $i < 4; $i++){
		if ($xml->weather->forecast_conditions->$i) {
			$forecast_day[] = $xml->weather->forecast_conditions->$i->day_of_week['data'];
			$forecast_condition[] = $xml->weather->forecast_conditions->$i->condition['data'];
			$low = $xml->weather->forecast_conditions->$i->low['data'];
			if ($units=='k')
				$low = round((($low - 32)*5/9) + 273, 1);
			else if ($units=='c')
				$low = round(($low - 32)*5/9, 1);
			$forecast_low[] = $low;
			$high = $xml->weather->forecast_conditions->$i->high['data'];
			if ($units=='k')
				$high = round((($high - 32)*5/9) + 273, 1);
			else if ($units=='c')
				$high = round(($high - 32)*5/9, 1);
			$forecast_high[] = $high;
			$forecast_icon[] = $xml->weather->forecast_conditions->$i->icon['data'];
		}
	}
	
	// current
	$condition = $xml->weather->current_conditions->condition['data'];
	$temp = $xml->weather->current_conditions->temp_f['data'];
	if ($units=='k')
		$temp = round((($temp - 32)*5/9) + 273, 1);
	else if ($units=='c')
		$temp = round(($temp - 32)*5/9, 1);
	$icon = $xml->weather->current_conditions->icon['data'];
	
	// build the HTML
	$header = "<tr><td colspan=\"".count($forecast_day)."\" bgcolor=\"#$header_color\">";
	if ($icon!='')
		$header .= "<img align=\"left\" src=\"http://www.google.com$icon\">";
	$header .= "<b>$location</b><br>Currently <i>$condition</i> <b>$temp$units</b>";
	$header .= "</td></tr>\n<tr>";
	$data = "<tr>\n";
	
	for ($i = 0; $i < count($forecast_day); $i++){
		$header .= "<td width=\"130\" bgcolor=\"#$day_header_color\"><b>$forecast_day[$i]</b></td>";
		$data .= "<td bgcolor=\"#$day_color\">";
		$data .= "<img src=\"http://www.google.com$forecast_icon[$i]\">";
		$data .= "<br><i>$forecast_condition[$i]</i>";
		$data .= "<br>high <b>$forecast_high[$i]$units</b>";
		$data .= "<br>low <b>$forecast_low[$i]$units</b>";
		$data .= "</td>\n";
	}
	
	$header .= "</tr>";
	$data .= "</tr>";
	
	return "<table cellpadding=\"5\" cellspacing=\"3\">\n$header\n$data\n</table>";
}

?>

eXorithm – Execute Algorithm: View / Run Algorithm ascii_art

function ascii_art ($img
{
  $width = imagesx$img);
  $height = imagesy$img);
  
  $ascii = '<pre style="font-size:8px;line-height:1px;letter-spacing:-2px;">'
  
  for$y=0;$y$height$y++)
  {
    for$x=0;$x$width$x++)
    {
      $c = imagecolorat$img$x$y);
      $r = ($c >> 16) & 0xFF;
      $g = ($c >> 8) & 0xFF;
      $b = $c & 0xFF;
      $t = ($c >> 24) & 0xFF;
      if ($t>=0)
      {
        /** transparency **/
        $r = $r+(255-$r)*$t/127;
        $g = $g+(255-$g)*$t/127;
        $b = $b+(255-$b)*$t/127;
      }
      $r = floor$r/16);
      $g = floor$g/16);
      $b = floor$b/16);
      $ascii .= '<span style="color:#'dechex$r).dechex$g).dechex$b).'">#</span>'
    }
    $ascii .= '<br>'
  }
  
  $ascii .= '</pre>'
  
  return $ascii

eXorithm – Execute Algorithm: View / Run Algorithm project_polygon

function project_polygon ($points, $degree_x, $degree_y, $degree_z, $center_x, $center_y, $center_z, $dist1, $dist2, $include_z
{
  // check points
  if ((count$points)%3)!=0) {
    throw new Exception'The points must be a list like x1, y1, z1, x2, y2, z2, etc. The number of points therefore must be divisible by three.');
  }
  
  $degree_x = deg2rad$degree_x);
  $degree_y = deg2rad$degree_y);
  $degree_z = deg2rad$degree_z);
  
  $cosx = cos$degree_x);
  $sinx = sin$degree_x);
  $cosy = cos$degree_y);
  $siny = sin$degree_y);
  $cosz = cos$degree_z);
  $sinz = sin$degree_z);
  
  $array = array();
  
  for ($i=0;$icount$points);$i$i+3) {
    $x0 = $points$i]-$center_x
    $y0 = $points$i+1]-$center_y
    $z0 = $points$i+2]-$center_z
    
    $x1 = $cosy*($sinz$y0 + $cosz$x0) - $siny$z0
    $y1 = $sinx*($cosy$z0 + $siny*($sinz$y0 + $cosz$x0)) + $cosx*($cosz$y0 - $sinz$x0);
    $z1 = $cosx*($cosy$z0 + $siny*($sinz$y0 + $cosz$x0)) - $sinx*($cosz$y0 - $sinz$x0);
  
    $x2 = $x1$dist1/($z1$dist1$dist2);
    $y2 = $y1$dist1/($z1$dist1$dist2);
    $z2 = $z1$dist1/($z1$dist1$dist2);
  
    $array[] = $x2
    $array[] = $y2
    if ($include_z) $array[] = $z2
  }
  
  return $array
} 

eXorithm – Execute Algorithm: History For Algorithm weather_forecast

weather_forecast     version 1.4     Display the weather for a location. Look up using the Google weather API.

Version Note Created Diff
1.4 [edit Reverted from version 1.1 Apr 23, 2012 09:40 am by Mike Campbell
1.3 [revert Mar 22, 2012 03:35 pm by jessiie
1.2 [revert Mar 22, 2012 03:24 pm by jessiie
1.1 [revert support bth celsius and kelvin Nov 23, 2011 01:43 am by Mike Campbell
1.0 [revert Celsius is now Kelvin Nov 20, 2011 11:32 pm by ekajjake
0.5 [revert Nov 20, 2011 11:32 pm by ekajjake
0.4 [revert Nov 20, 2011 11:31 pm by ekajjake
0.3 [revert Nov 20, 2011 11:30 pm by ekajjake
0.2 [revert Dec 1, 2010 02:34 pm by Mike Campbell
0.1 [revert Oct 27, 2010 02:02 pm by Mike Campbell

eXorithm – Execute Algorithm: View / Run Algorithm word_counts

function word_counts ($text, $noise
{
  $words = preg_split'/[^A-Za-z]+/', strtolower$text));
  $counts = array();
  
  foreach ($words as $word) {
    if (strlen$word)>1) { // 1-letter words are ignored
      if (array_search$word, $noise)===false) { // noise word?
        if (array_key_exists$word, $counts)) {
          $counts$word] = $counts$word]+1;
        } else {
          $counts$word] = 1;
        }
      }
    }
  }
  
  return $counts
}Â