[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
$ sudo yum install \
gcc.`uname -i` \
libxml2-devel.`uname -i` \
httpd-devel.`uname -i` \
openssl-devel.`uname -i` \
libjpeg-turbo-devel.`uname -i` \
libcurl-devel.`uname -i` \
libpng-devel.`uname -i` \
libmcrypt-devel.`uname -i` \
mysql-devel.`uname -i` \
readline-devel.`uname -i`
$ cd /usr/local/src
$ sudo curl -L http://jp1.php.net/get/php-5.6.9.tar.bz2/from/this/mirror -o php-5.6.9.tar.bz2
$ sudo tar xvjf php-5.6.9.tar.bz2
$ cd php-5.6.9
$ sudo ./configure \
--with-apxs2 \
--with-libdir=lib64 \
--disable-cgi \
--with-openssl=shared \
--with-zlib=shared \
--with-curl=shared \
--enable-ftp=shared \
--with-gd=shared \
--with-jpeg-dir \
--with-png-dir \
--enable-gd-native-ttf \
--enable-mbstring=shared \
--with-mcrypt=shared \
--with-mysql=shared \
--with-mysqli=shared \
--with-pdo-mysql=shared \
--with-pdo-sqlite=shared \
--enable-zip \
--with-readline
$ sudo make
$ sudo make test
$ sudo make install
--enable-sqlite-utf8 --enable-zend-multibyte
libjpeg-devel -> libjpeg-turbo-devel curl-devel -> libcurl-devel
(function() { /* ... */ })();
current([function($var1, $var2) {
var_dump($var1);
var_dump($var2);
}])->__invoke($a, $b);
call_user_func(function($var1, $var2) {
var_dump($var1);
var_dump($var2);
}, $a, $b);
「閲覧当日の時点で、特定の生徒が年度内に特定のコンテンツを複数回閲覧しているかどうか、閲覧履歴の作成日を見て判定する。"年度内"の判定は、直近の年度開始日以降(閲覧当日よりも前にある日付)の日付を計算して行う」
最近上記のような処理を実装する機会があった。実装当初は、次の2つの処理を一つのメソッドにまとめようとしていた。1つ目の処理については汎用的に使いまわす可能性があり、何よりもメソッド内が複雑になりそうだと感じ、処理を別の関数とメソッドに切り出す事にした。二つ目の処理はさておき、「今日の日付から年度の開始日を算出する処理」の関数の命名ではまった、というか悩んだ。
次に年度の開始日を求めるメソッドをクラスに定義するにあたり、メソッド名をいくつか候補に挙げてみた。
自身の英語力が貧弱という事もあり、しっくりくる名前が思い浮かばない。
少し悩んでから、そもそも「年度内〜」という単語に意識が引きずられている事に気づいた。
期間を算出する処理なので、この処理自身が年度の概念を知っている必要はない。なので、「直近の過去で一番近くに「指定した月」に該当する年月」を取得する、とだけ考えると役割をシンプルに捉えられそうだと思い至った。それでも、ややこしい気がするけれども、「年度」という言葉を排除するだけでも随分印象はさっぱりする。
最終的に、クラスには定義せず、"findMostRecentDate()" という関数を定義した。「最も直近の日付」といった意味になるだろう。おそらく。
本当は「前の」「過去の」といった意味合いも含めるべきかもしれないけれど、適切な表現が思い浮かばなかったので省略し、用例をコメントに含めて関数の挙動を説明することにした。
/**
* 指定した日付($date)から過去に遡って、基準月($month_cond)が最初に登場する年月を返す
* 例) findMostRecentDate('2014-05-11', 4) -> 2014-04-01
* findMostRecentDate('2014-03-01', 4) -> 2013-04-01
* findMostRecentDate('2017-11-24', 10) -> 2017-10-01
* findMostRecentDate('2015-01-01', 10) -> 2014-10-01
*
* @param string $date YYYY-MM-DD形式の日付
* @param int $month_cond 基準月
*/
function findMostRecentDate($date, $month_cond)
{
if (!$date || !$month_cond) return null;
$ts = strtotime($date);
$year = date('Y', $ts);
$c = 3; //適当なところで抜ける
while($c>0) {
// mktime(hour, min, sec, month,day, year );
$ts = mktime(null, null, null, $month_cond, 1, $year);
$recent = date('Y-m-01', $ts);
if ($recent < $date) return $recent;
$year--;
$c--;
}
return null;
}
<?php
$values = array(
0,
1,
1237,
'1237',
'1234',
null,
'',
' ',
' ',
'-',
'a',
'*',
'あ',
);
foreach($values as $v) {
echo number_format($v), "\n";
//echo "raw :'", $v, "'\n";
//echo "double:", number_format((double) $v, 5), "\n";
//echo "float :", number_format((float) $v, 5), "\n";
//echo "int :", number_format((int) $v, 5), "\n";
}
0 1 1,237 1,237 0 0 0 0 0 0 0 0 0
0 1 1,237 1,237 PHP Warning: number_format() expects parameter 1 to be double, string given in /home/hato/work/sandbox/php/number_format.php on line 21 Warning: number_format() expects parameter 1 to be double, string given in /home/hato/work/sandbox/php/number_format.php on line 21 0 PHP Warning: number_format() expects parameter 1 to be double, string given in /home/hato/work/sandbox/php/number_format.php on line 21 Warning: number_format() expects parameter 1 to be double, string given in /home/hato/work/sandbox/php/number_format.php on line 21 PHP Warning: number_format() expects parameter 1 to be double, string given in /home/hato/work/sandbox/php/number_format.php on line 21 Warning: number_format() expects parameter 1 to be double, string given in /home/hato/work/sandbox/php/number_format.php on line 21 PHP Warning: number_format() expects parameter 1 to be double, string given in /home/hato/work/sandbox/php/number_format.php on line 21 Warning: number_format() expects parameter 1 to be double, string given in /home/hato/work/sandbox/php/number_format.php on line 21 PHP Warning: number_format() expects parameter 1 to be double, string given in /home/hato/work/sandbox/php/number_format.php on line 21 Warning: number_format() expects parameter 1 to be double, string given in /home/hato/work/sandbox/php/number_format.php on line 21 PHP Warning: number_format() expects parameter 1 to be double, string given in /home/hato/work/sandbox/php/number_format.php on line 21 Warning: number_format() expects parameter 1 to be double, string given in /home/hato/work/sandbox/php/number_format.php on line 21 PHP Warning: number_format() expects parameter 1 to be double, string given in /home/hato/work/sandbox/php/number_format.php on line 21 Warning: number_format() expects parameter 1 to be double, string given in /home/hato/work/sandbox/php/number_format.php on line 21 PHP Warning: number_format() expects parameter 1 to be double, string given in /home/hato/work/sandbox/php/number_format.php on line 21 Warning: number_format() expects parameter 1 to be double, string given in /home/hato/work/sandbox/php/number_format.php on line 21
連想配列の配列を、複数の列でソートしたい場合、array_multisortが利用できます。
関数の引数と指定順は以下の通りです。
bool array_multisort ( array &$array1 [, mixed$array1_sort_order = SORT_ASC [, mixed$array1_sort_flags = SORT_REGULAR [, mixed$... ]]] )URL: http://jp2.php.net/manual/ja/function.array-multisort.php#refsect1-function.array-multisort-description
配列を複数指定した場合は、前の配列と要素(の位置)が対応する配列は同時にソートされます・・・分かり辛いので、実際にコードを書いて動作を確認してみます。
<?php $ary1 = array( 1, 2, 3, 4); $ary2 = array(100, 50, 1, 0); $result = array_multisort($ary1, SORT_DESC, $ary2); var_dump($ary1); var_dump($ary2);
実行結果)
array(4) {
[0]=> int(4)
[1]=> int(3)
[2]=> int(2)
[3]=> int(1)
}
array(4) {
[0]=> int(0)
[1]=> int(1)
[2]=> int(50)
[3]=> int(100)
}
次にタイトルの通り、複数の値を基準にソートする処理を書いてみます。
<?php
//連想配列の配列を定義します
$rows = array(
array('id' => 3, 'date' => '2014-02-20', 'a' => '3', 'b' => 'A'),
array('id' => 4, 'date' => '2014-09-20', 'a' => '4', 'b' => 'B'),
array('id' => 2, 'date' => '2014-10-20', 'a' => '2', 'b' => 'C'),
array('id' => 8, 'date' => '2014-09-20', 'a' => '8', 'b' => 'A'),
array('id' => 1, 'date' => '2014-11-01', 'a' => '1', 'b' => 'D'),
array('id' => 5, 'date' => '2013-04-10', 'a' => '5', 'b' => 'E'),
array('id' => 6, 'date' => '2014-09-20', 'a' => '6', 'b' => 'F'),
array('id' => 7, 'date' => null , 'a' => '7', 'b' => 'F'),
);
//ソートに用いる要素を一次元配列にまとめます。
$ids = array();
$dates = array();
foreach ($rows as $k => $row) {
$ids[$k] = $row['id'];
$dates[$k] = strtotime($row['date']);
}
//date列の降順、かつidの昇順でソートします。
//引数は、ソートする配列
//
array_multisort($dates, SORT_DESC, $ids, SORT_ASC, $rows);
//結果を出力します。
echo "\n";
foreach ($rows as $row) {
echo sprintf("id:%d, date:%10s, a:%s, b:%s", $row['id'], $row['date'], $row['a'], $row['b']), "\n";
}
id:1, date:2014-11-01, a:1, b:D id:2, date:2014-10-20, a:2, b:C id:4, date:2014-09-20, a:4, b:B id:6, date:2014-09-20, a:6, b:F id:8, date:2014-09-20, a:8, b:A id:3, date:2014-02-20, a:3, b:A id:5, date:2013-04-10, a:5, b:E id:7, date: , a:7, b:F
多次元の配列をソートする場合、ソート用に配列を作る手間が増えます。その分、一次元配列の場合と比べて面倒かもしれませんね・・・・