开发者

Better code for PHP associative array [closed]

开发者 https://www.devze.com 2023-03-17 22:16 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

How can I make this compact?

function cake_decode( ) {

$n = "BB-005";
$arr_cake_code = array(
"GR" => "Groom's Cake",
"WD"开发者_如何学Go => "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);


Put your data in JSON, put it in a file, and load it from there, since it's a resource. That'll reduce the size of your code.


Well, this is shorter:

$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" });

But seriously... don't bother. It's about as short as it can get in your own example.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号