在数组中通过索引引用静态字符串


Reference a static string by index in an array?

我有一个if语句求2(数字1和0);

$output['mustHaveButtonText'] = ($dealer->mustHave == 0) ? 'Add to Must Have' : 'Remove From Must Have';

我想要的是将数字赋值给字符串然后显示字符串而不是数字,

 0 =  "Must Have";  
 1 = "Must Not Have";

如果我理解正确的话,您想要的是一个数组,如下所示:

$text = array( 0 => 'Add to Must Have', 1 => 'Remove from Must Have');

那么你可以写:

$output['mustHaveButtonText'] = $text[$dealer->mustHave];

这样,即使您多次需要该文本,您也只需要定义它一次,但可以多次使用它。