Sunday, September 20, 2009

How to send mail with binary word in mail subject using PHP


Please refer RFC 2047 for detail format.

For simple, if your mail subject contains binary, and I use UTF-8 as a example:
$subject = '=?UTF-8?B?'.base64_encode($subject).'?=';

Sunday, September 13, 2009

Linq Group By 2 columns


from c in db.Customers

group c by new { c.Country, c.City } into g

select new { g.Key.Country, g.Key.City, Count = g.Count() };

Friday, September 11, 2009

Left join in LINQ


In SQL:
SELECT * FROM table_A LEFT JOIN table_B WHERE table_A.col=table_B.col

In LINQ:
from a in table_A
join b in table_B on a.col equals b.col into temp
from finalSet in temp.DefaultIfEmpty()


hightlighted is the keywords that using left join in linq
seems using a temp dataset the set the record and fill the missing part in table_B with empty to get the finalSet.

Thursday, September 10, 2009

How to remove duplicated 2-dimension array in PHP?


My sample array Data:

$arr[] = array(0,1,2,3);
$arr[] = array(4,5,2,1);
$arr[] = array(0,0,0,0);
$arr[] = array(0,1,2,3);

I expected to return

array(array(0,1,2,3), array(4,5,2,1), array(0,0,0,0));
Solution:
$arr = array_map('unserialize', array_unique(array_map('serialize', $arr)));

Wednesday, September 9, 2009

When to prefer JSON over XML?


Favor XML over JSON when any of these is true:

  • You need message validation
  • You're using XSLT
  • Your messages include a lot of marked-up text
  • You need to interoperate with environments that don't support JSON

Favor JSON over XML when all of these are true:

  • Messages don't need to be validated, or validating their deserialization is simple
  • You're not transforming messages, or transforming their deserialization is simple
  • Your messages are mostly data, not marked-up text
  • The messaging endpoints have good JSON tools