更好的PHP关联数组代码

时间:2011-07-07 20:38:41

标签: php arrays

我怎样才能制作这款小巧的?

function cake_decode( ) {

$n = "BB-005";
$arr_cake_code = array(
"GR" => "Groom's Cake",
"WD" => "Deluxe Cake",
"WC" => "Custom Cake",
"BR" => "Bridal Shower Cake",
"BB" => "Baby Shower Cake",
"RC" => "Religious Cake",
"ST" => "Sport Themes Cake",
"SP" => "Special Occasion Cake",
"GC" => "Graduation Cake",
"CB" => "Child Birthday Cake",
"BD" => "Adult Birthday Cake",
"AN" => "Anniversary Cake",
"VC" => "Valentine's Day Cake",
"THX" => "Thanksgiving Cake",
"NY" => "New Year's Cake",
"HC" => "Easter Cake",
"HW" => "Halloween Cake",
"CH" => "Christmas Cake",
"JC" => "4th of July Cake",
"CC" => "Dessert",
"CO" => "Corporate Cake",
"SC" => "Scene Cake"
);

//split $n
$o = explode("-", $n);
$p = $arr_cake_code[$o[0]];
echo "$p: $n";
}

cake_decode($ n)的;

2 个答案:

答案 0 :(得分:1)

将您的数据放入JSON中,将其放入文件中,然后从那里加载,因为它是一种资源。这将减少代码的大小。

答案 1 :(得分:0)

嗯,这个更短:

$arr_cake_code = array(
"GR" => "Groom's",
"WD" => "Deluxe",
"WC" => "Custom",
"BR" => "Bridal Shower",
"BB" => "Baby Shower",
"RC" => "Religious",
"ST" => "Sport Themes",
"SP" => "Special Occasion",
"GC" => "Graduation",
"CB" => "Child Birthday",
"BD" => "Adult Birthday",
"AN" => "Anniversary",
"VC" => "Valentine's Day",
"THX" => "Thanksgiving",
"NY" => "New Year's",
"HC" => "Easter",
"HW" => "Halloween",
"CH" => "Christmas",
"JC" => "4th of July",
"CC" => "Dessert",
"CO" => "Corporate",
"SC" => "Scene"
);

array_walk($arr_cake_code, function(&$item, $key) { $item=$item . " Cake" });

但严重的是......不要打扰。它与你自己的例子中的一样短。