mysql 根据经纬度和距离筛选数据

先根据经纬度和距离计算一个矩形,然后筛选经纬度在矩形范围内的数据。

/**
 *
 * @param Select $select            
 * @param string $columnName            
 * @param string $longitude            
 * @param string $latitude            
 * @param int $distance
 *            metre
 * @return string
 */
static function ST_MBRWithinRectangle(Select $select, $columnName, 
    $longitude, $latitude, $distance)
{
    list ($min, $max) = self::rectangle($longitude, $latitude, $distance);
    $where = "$columnName!=PointFromText('Point(0 0)') and $columnName!='' and";
    $where .= " ST_MBRWithin(`$columnName`,ST_GeomFromText('Polygon(($min[0] $min[1],";
    $where .= "$min[0] $max[1],$max[0] $max[1],$max[0] $min[1],$min[0] $min[1]))'))";
    $select->where($where);
}

/**
 * get rectangle longitude and latitude
 *
 * @param float $longitude
 * @param float $latitude
 * @param integer $distance
 *            meter
 * @return array
 */
static function rectangle($longitude, $latitude, $distance)
{
    $radius = 6371 * 1000;
    // latitude boundaries
    $maxlat = $latitude + rad2deg($distance / $radius);
    $minlat = $latitude - rad2deg($distance / $radius);
    // longitude boundaries (longitude gets smaller when latitude increases)
    $maxlng = $longitude +
         rad2deg($distance / $radius / cos(deg2rad($latitude)));
    $minlng = $longitude -
         rad2deg($distance / $radius / cos(deg2rad($latitude)));
    return array(
        array(
            'lng' => $minlng,
            'lat' => $minlat
        ),
        array(
            'lng' => $maxlng,
            'lat' => $maxlat
        )
    );
}

 

Mac ZendStudio 字体过小问题解决

Mac下ZendStudio有个问题,项目列表字体巨小无比,大显示器上没法看。解决办法如下:

Finder->应用程序 找到 Zend Studio点右键,显示包内容,进入Contents->Eclipse,编辑ZendStudio.ini,找到

-Dorg.eclipse.swt.internal.carbon.smallFonts

这一行,最前面加#注释掉,重启Zend Studio即可。

PHP无限分组

PHP高级技术群:424844502

世界上最好的语言不是吹的,轻松实现一个令人叹为观止的无限级别分组列表。

/**
 * group by any level
 * http://blog.phpdr.net/php%E6%97%A0%E6%9E%81%E5%88%86%E7%BB%84.html
 *
 * @param array $list
 * @param mixed $columns
 * @param mixed $group
 * @param mixed $primary
 * @return array
 */
function dict(array $list, $columns = null, $group = null, $primary = null) {
	if (! isset ( $primary )) {
		$primary = array ();
	}
	if (is_string ( $primary )) {
		$primary = array (
				$primary
		);
	}
	if (is_string ( $group )) {
		$group = array (
				$group
		);
	}
	$listNew = array ();
	foreach ( $list as $v ) {
		$key = null;
		foreach ( $primary as $v1 ) {
			$key .= $v [$v1];
		}
		if (isset ( $columns )) {
			if (is_array ( $columns ) && 1 === count ( $columns ) && $columns [0] === null) {
				$vNew = null;
			} else {
				$vNew = array ();
				if (is_array ( $columns )) {
					foreach ( $columns as $k1 => $v1 ) {
						if (is_int ( $k1 )) {
							$k1 = $v1;
						}
						$vNew [$k1] = $v [$v1];
					}
				} else {
					$vNew = $v [$columns];
				}
			}
		} else {
			$vNew = $v;
		}
		if (isset ( $group )) {
			$vGroup = &$listNew;
			foreach ( $group as $v2 ) {
				if (isset ( $vGroup ) && array_key_exists ( $v [$v2], $vGroup )) {
					$vGroup = &$vGroup [$v [$v2]];
				} else {
					$vGroup [$v [$v2]] = array ();
					$vGroup = &$vGroup [$v [$v2]];
				}
			}
			if (isset ( $key )) {
				$vGroup [$key] = $vNew;
			} else {
				$vGroup [] = $vNew;
			}
		} else {
			if (isset ( $key )) {
				$listNew [$key] = $vNew;
			} else {
				$listNew [] = $vNew;
			}
		}
	}
	return $listNew;
}

$list = array ();
$list [] = array (
		'id' => 1,
		'name' => '学生1',
		'school' => '学校1',
		'class' => '班级1'
);
$list [] = array (
		'id' => 4,
		'name' => '学生4',
		'school' => '学校2',
		'class' => '班级2'
);
$list [] = array (
		'id' => 2,
		'name' => '学生2',
		'school' => '学校1',
		'class' => '班级2'
);
$list [] = array (
		'id' => 3,
		'name' => '学生3',
		'school' => '学校2',
		'class' => '班级1'
);
$list [] = array (
		'id' => 5,
		'name' => '学生5',
		'school' => '学校2',
		'class' => '班级3'
);
echo '<pre>';
print_r ( dict ( $list, 'name' ) );
var_dump ( dict ( $list, array (
		null
) ) );
print_r ( dict ( $list, array (
		'id',
		'name',
		'school'
), null, 'id' ) );
print_r ( dict ( $list, null, null, array (
		'class',
		'name'
) ) );
print_r ( dict ( $list, array (
		'name'
), 'school', 'id' ) );
print_r ( dict ( $list, array (
		'name'
), array (
		'school',
		'class'
), 'id' ) );
print_r ( dict ( $list, 'name' ) );

Array
(
    [0] => 学生1
    [1] => 学生4
    [2] => 学生2
    [3] => 学生3
    [4] => 学生5
)
var_dump ( dict ( $list, array (
		null
) ) );

array(5) {
  [0]=>
  NULL
  [1]=>
  NULL
  [2]=>
  NULL
  [3]=>
  NULL
  [4]=>
  NULL
}
print_r ( dict ( $list, array (
		'id',
		'name',
		'school'
), null, 'id' ) );

Array
(
    [1] => Array
        (
            [id] => 1
            [name] => 学生1
            [school] => 学校1
        )

    [4] => Array
        (
            [id] => 4
            [name] => 学生4
            [school] => 学校2
        )

    [2] => Array
        (
            [id] => 2
            [name] => 学生2
            [school] => 学校1
        )

    [3] => Array
        (
            [id] => 3
            [name] => 学生3
            [school] => 学校2
        )

    [5] => Array
        (
            [id] => 5
            [name] => 学生5
            [school] => 学校2
        )

)
print_r ( dict ( $list, null, null, array (
		'class',
		'name'
) ) );

Array
(
    [班级1学生1] => Array
        (
            [id] => 1
            [name] => 学生1
            [school] => 学校1
            [class] => 班级1
        )

    [班级2学生4] => Array
        (
            [id] => 4
            [name] => 学生4
            [school] => 学校2
            [class] => 班级2
        )

    [班级2学生2] => Array
        (
            [id] => 2
            [name] => 学生2
            [school] => 学校1
            [class] => 班级2
        )

    [班级1学生3] => Array
        (
            [id] => 3
            [name] => 学生3
            [school] => 学校2
            [class] => 班级1
        )

    [班级3学生5] => Array
        (
            [id] => 5
            [name] => 学生5
            [school] => 学校2
            [class] => 班级3
        )

)
print_r ( dict ( $list, array (
		'name'
), 'school', 'id' ) );

Array
(
    [学校1] => Array
        (
            [1] => Array
                (
                    [name] => 学生1
                )

            [2] => Array
                (
                    [name] => 学生2
                )

        )

    [学校2] => Array
        (
            [4] => Array
                (
                    [name] => 学生4
                )

            [3] => Array
                (
                    [name] => 学生3
                )

            [5] => Array
                (
                    [name] => 学生5
                )

        )

)
print_r ( dict ( $list, array (
		'name'
), array (
		'school',
		'class'
), 'id' ) );

Array
(
    [学校1] => Array
        (
            [班级1] => Array
                (
                    [1] => Array
                        (
                            [name] => 学生1
                        )

                )

            [班级2] => Array
                (
                    [2] => Array
                        (
                            [name] => 学生2
                        )

                )

        )

    [学校2] => Array
        (
            [班级2] => Array
                (
                    [4] => Array
                        (
                            [name] => 学生4
                        )

                )

            [班级1] => Array
                (
                    [3] => Array
                        (
                            [name] => 学生3
                        )

                )

            [班级3] => Array
                (
                    [5] => Array
                        (
                            [name] => 学生5
                        )

                )

        )

)