像windows那样管理你的桌面:Spectacle

Mac OS自身的窗口平铺管理和渣到不行的多显示屏支持一直是我不满意的地方,今天在网上闲逛,看到有人介绍了一款名为Spectacle的应用,可以让Mac像Windows那样对窗口进行各种平铺,于是试用了一下,学会一系列的快捷键后,我已经离不开它了。下面是我从应用Github页上摘录的内容:

Windows can be moved to a number of predefined regions of the screen:

Move to the left half — ⌥⌘←
Move to the right half — ⌥⌘→
Move to the top half — ⌥⌘↑
Move to the bottom half — ⌥⌘↓

Move to the upper left — ⌃⌘←
Move to the lower left — ⌃⇧⌘←
Move to the upper right — ⌃⌘→
Move to the lower right — ⌃⇧⌘→

看起来有点复杂,但用着十分顺手,可以方便地让窗口占满半屏、全屏、三分之一屏和四分之一屏,用好了绝对大大地提升你的工作效率。

Spectacle

官方网站

如何得到两个日期中间所有日期的Array

如果我有两个日期的String,’2015-01-01′ 和 ‘2016-03-15’, 如何使用PHP将它们之间的所有日期用Array封装呢?PHP有一个DateInterval的Class就是做这个的。下面是我根据PHP官方文档写的function。

function get_date_array($start_date, $end_date){
    $date_array = array();
    $start_date = new DateTime($start_date);
    $end_date = new DateTime($end_date);
    //$end_date = $end_date->modify(' +1 day ');//如果要包括end_date的话
    $interval = new DateInterval('P1D'); 
    $daterange = new DatePeriod($start_date, $interval ,$end_date);
    foreach($daterange as $date){
         array_push($date_array, $date->format("Y-m-d"));
    }
    return $date_array;
}

PHP:如何过滤一个Array

$new_array = array('2015-01-01', '2015-12-01', '2016-01-01','2016-05-05','2016-07-01');

给出上面这样一个array,如何过滤它,得出2015-06-01之后的值呢?可以用array_filter 这个function。下面是使用的例子

$filtered_array = array_filter($new_array, 'my_filter_function');
function my_filter_function($var){
    return strtotime($var)>=strtotime('2015-06-01');
}

PHP:json_encode返回的可不一定是json object

用PHP做移动端数据接口,以json_encode返回json object数据,今天安卓端的同事跟我反映收到一个空的Array,导致客户端崩溃。空的Array在json_encode的时候难道不会被转为json object么?

查了一下还真是这样。如果不加任何选项,空的Array默认返回的还是空Array,想要全部转为json object,必须加JSON_FORCE_OBJECT选项作为第二参数。

下面是php官网摘录的具体选项的用法

<?php
$a = array('<foo>',"'bar'",'"baz"','&blong&', "xc3xa9");

echo "Normal: ",  json_encode($a), "n";
echo "Tags: ",    json_encode($a, JSON_HEX_TAG), "n";
echo "Apos: ",    json_encode($a, JSON_HEX_APOS), "n";
echo "Quot: ",    json_encode($a, JSON_HEX_QUOT), "n";
echo "Amp: ",     json_encode($a, JSON_HEX_AMP), "n";
echo "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "n";
echo "All: ",     json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), "nn";

$b = array();

echo "Empty array output as array: ", json_encode($b), "n";
echo "Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "nn";

$c = array(array(1,2,3));

echo "Non-associative array output as array: ", json_encode($c), "n";
echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT), "nn";

$d = array('foo' => 'bar', 'baz' => 'long');

echo "Associative array always output as object: ", json_encode($d), "n";
echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "nn";
?>

下面是对应的output


Normal: ["","'bar'",""baz"","&blong&","u00e9"]
Tags: ["u003Cfoou003E","'bar'",""baz"","&blong&","u00e9"]
Apos: ["","u0027baru0027",""baz"","&blong&","u00e9"]
Quot: ["","'bar'","u0022bazu0022","&blong&","u00e9"]
Amp: ["","'bar'",""baz"","u0026blongu0026","u00e9"]
Unicode: ["","'bar'",""baz"","&blong&","é"]
All: ["u003Cfoou003E","u0027baru0027","u0022bazu0022","u0026blongu0026","é"]

Empty array output as array: []
Empty array output as object: {}

Non-associative array output as array: [[1,2,3]]
Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}

Associative array always output as object: {"foo":"bar","baz":"long"}
Associative array always output as object: {"foo":"bar","baz":"long"}

我觉得这个是一个很好的PHP面试题。