2

I already tried this:

<?php
function checkForOrange($farben) {
     foreach($farben as $element) {
            if(in_array('orange',$element) || in_array('Orange',$element)) {
           return $so;
      }
      return false;
}
}
$so = checkForOrange($farben);
if (!empty($so)) { echo "nothing"; }
if (isset($so)) { echo "daa"; }
?>

but it always returns "daa" even if i check for sfefrsefdeas. Can someone please give me a working code for this?

3
  • Try the check as if(!isset($so) rather then empty. Also in checkForOrange where is the definition of $so? Commented Sep 18, 2013 at 8:43
  • $so is always set, when you explicetly declare it as $so = checkForOrange. In other words - you already set the variable. Commented Sep 18, 2013 at 9:03
  • i tryed if (!isset($so)) { echo "daa"; } and removed if (!empty... but it doesn't work Commented Sep 18, 2013 at 10:48

3 Answers 3

3

Function to check existence of value in multidimensional array.

Function return true or false, you can use it in general.

function search_in_array($value, $array) {
     if(in_array($value, $array)) {
          return true;
     }
     foreach($array as $item) {
          if(is_array($item) && search_in_array($value, $item))
               return true;
     }
   return false;
}

Working example

--------- edit ----------- Based on your comment, and an example of an array

function check_in_array($value, $array, $key){
 foreach($array as $item){
  if($item[$key] == $value)
  return true;
  }
 return false;
}

And then call it like this check_in_array('orange', $array, 'FacetValueName');

Or this

check_in_array('orange', $array, 'FacetValueName') && check_in_array('Orange', $array, 'FacetValueName') && check_in_array('dark orange', $array, 'FacetValueName')

to check multiple values at once.

Working example

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

5 Comments

Your function is unnecessary big and same way recursive
Perhaps, but not necessarily. I dont have the example of array he is searching in (how much multidimensional, structure), and no idea how he use this in the big picture (some function, or new behavior in some eshop?). So I decided to provide general answer.
Thanks but it's checking only for one value, i need to check for two or more values and if only one or all of them match (example: "orange" or "Orange" or "dark orange") then return true
Could you post the whole code in the sandbox please? I don't know what comes in the row if($item[$key] == $value) if there have to be multiple values
Here sandbox.onlinephpfunctions.com/code/… , look at the $so variable, and undrestand how to use this function to check multiple values.
0

That's how I see it. Assuming you are comparing in_array to the foreached value, you are having a multidimensional array. So I basically created two 2d arrays. One called $farben1 and the second $farben2. In the first one key 'fruits' => 0=> 'Apple', 1=>'Orange', 2=>'Lemon'; while the second has Melon instead of orange. So it does not contains the word 'orange'.

If think you can just compare if the lowered value of $element is 'orange', so I assigned strtolower() to your array $element.

<?php
function checkForOrange(array $farben) {
     foreach($farben as $element) {
         $bool[] = in_array('orange', array_map('strtolower', $element));
     }
     return in_array(TRUE, $bool);
}

$farben1['animals'] = array('cat', 'Dog', 'Lion');
$farben1['fruits'] = array('Apple', 'Orange', 'Lemon'); //contains
$farben1['flowers'] = array('Roses', 'violets');

echo !checkForOrange($farben1) ? 'nothing' : 'daaa'; // prints daaa

echo "<br /> <br />";

$farben2['animals'] = array('cat', 'Dog', 'Lion');
$farben2['fruits'] = array('Apple', 'Melon', 'Lemon'); //does not contain
$farben2['flowers'] = array('Roses', 'violets');

echo !checkForOrange($farben2) ? 'nothing' : 'daaa'; // prints Nothing

?>

Comments

0

Here is the function i made to check if value is exist in multi-dimension array.

function is_val_exists($needle, $haystack) {
     if(in_array($needle, $haystack)) {
          return true;
     }
     foreach($haystack as $element) {
          if(is_array($element) && is_val_exists($needle, $element))
               return true;
     }
   return false;
}

You can use it like:

is_val_exists('your_value', $your_array);

It will return true if your_value is exists in $your_array.

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.