我有变量let,$ingredients说,它包含
面粉,糖,奶油,植物脂肪和油,鸡蛋,玉米淀粉,蛋糕OMath,葡萄糖,
乳清粉,可可粉,乳糖,全奶粉,盐,脱脂奶粉,
糊精,咖啡粉,黄油油,加糖浓缩脱脂牛奶,干蛋黄,
发酵剂,色素(焦糖,甘露糖和胡萝卜素),乳化剂(大豆起源),香精。
现在我想对它的$ingredient进行分组
(echo)的“好产品”。(else echo "Best product"。如何用php对其进行编码?
非常感谢
发布于 2014-01-26 13:40:53
使用数组()检查某项是否在数组中。
$ingredients = array('First ingredient', 'Second ingredient');如果成分是由逗号分隔的字符串,则可以使用以下方法将它们转换为数组:
$ingredients = explode(',',$ingredients);您可能需要修剪每一项,以确保删除每一项周围的任何空格(这将扰乱您的in_array()检查):
$ingredientsTrimmed = array();
foreach($ingredients as $ingredient)
{
$ingredientsTrimmed[] = trim($ingredient);
}
$ingredients = $ingredientsTrimmed;最后,您可以进行检查:
if(in_array('First ingredient',$ingredients))
{
// First ingredient is in the array
}若要检查数组是否同时包含这两种内容,请执行以下操作:
if(in_array('First ingredient',$ingredients) AND in_array('Second ingredient',$ingredients))
{
// First and second ingredient is in the array
}若要检查它是否包含一个或另一个,请执行以下操作:
if(in_array('First ingredient',$ingredients) || in_array('Second ingredient',$ingredients))
{
// First or second ingredient is in the array
}您可以根据需要添加多少‘和’,查看有关PHP逻辑运算符的更多信息。
发布于 2014-01-26 13:44:15
如果你的食材是串的:
if ( strpos($ingredients, "emulsifier") === false
&& strpos($ingredients, "shortening") === false ) {
echo 'Best Product';
} elseif ( strpos($ingredients, "emulsifier") !== false
&& strpos($ingredients, "shortening") !== false ) {
echo 'Good Product';
}https://stackoverflow.com/questions/21364051
复制相似问题