0

I have variable let says $ingredients which contains

Flour, sugar, shortening, plant fats and oils, egg, cornstarch, cake OMath, dextrose,

whey powder, cocoa powder, lactose, whole milk powder, salt, powdered skimmed milk,

dextrin, coffee powder, butter oil, sweetened condensed skimmed milk, dried egg yolk,

leavening agent, colouring (caramel, annatto and carotene), emulsifier (soybean origin), fragrances.

Now I want to grouping its $ingredient

  1. If $ingredients contain emulsifier and shortening, it wil shows (echo) "Good Product"

  2. However if there is no emulsifier and shortening, it is "Best product" (else echo "Best product"

How to code this in php ?

Thank you very much

1
  • Is $ingredients an array or a string? Commented Jan 26, 2014 at 13:39

2 Answers 2

2

Use in_array() to check if an item is in an array.

$ingredients = array('First ingredient', 'Second ingredient');

If the ingredients are a string separated by a comma you can convert them to an array using:

$ingredients = explode(',',$ingredients);

You may want to trim each item to ensure any whitespace around each item is removed (which will mess up your in_array() check:

$ingredientsTrimmed = array();

foreach($ingredients as $ingredient)
{
    $ingredientsTrimmed[] = trim($ingredient);
}

$ingredients = $ingredientsTrimmed;

Finally, you can do your checks:

if(in_array('First ingredient',$ingredients))
{
    // First ingredient is in the array
}

To check if an array contains both:

if(in_array('First ingredient',$ingredients) AND in_array('Second ingredient',$ingredients))
{
    // First and second ingredient is in the array
}

To check if it contains either one or the other:

if(in_array('First ingredient',$ingredients) || in_array('Second ingredient',$ingredients))
{
    // First or second ingredient is in the array
}

You can add as many 'AND's and '||'s as you need. See more about PHP's logical operators

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much. It is really true. How about if I want to add comparison, the strign is more that one. For example If $ingredients containc First ingredient or Second ingredient or Third ingredient, this good product. Thank you very much
You can create arrays: $good_elements, $bad_elements and use array_intersect().
I am sorry, i still have a problem. How to convert all ingredients to be array ? because I got as string as {Flour, sugar, shortening, plant fats and oils, egg, cornstarch, cake OMath, dextrose,whey powder, cocoa powder, lactose, whole milk powder, salt, powdered skimmed milk,dextrin, coffee powder, butter oil, sweetened condensed skimmed milk, dried egg yolk, leavening agent, colouring (caramel, annatto and carotene), emulsifier (soybean origin), fragrances} in single string called $ingredients. thanks
See updated answer, that should work so long as all the ingredients in your string are separated by a comma. If those curly brackets are at the start and end of your string, you'll need to strip those out. Otherwise the first and last element will contain the brackets too.
If you are checking for multiple ingredients, you can use: if ( !count(array_diff(array_intersect($ingredients, $good), $good)) ) to check ich all good ingredients are in array.
0

If your ingredients are in string:

if ( strpos($ingredients, "emulsifier") === false
  && strpos($ingredients, "shortening") === false ) {
   echo 'Best Product';
} elseif ( strpos($ingredients, "emulsifier") !== false
  && strpos($ingredients, "shortening") !== false ) {
   echo 'Good Product';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.