0

i have a problem with a php if condition

i have follow variables and arrays:

<?php
$appartamenti = array("97", "98", "99", "100");
$appartamentinoloft = array("97", "98", "99");
$case = array("103", "104", "107", "108");
$casevacanze = array("109", "110", "111", "112");
$stanze = array("115", "116");
$uffici = array("113", "114");
$locali = array("117", "118");
$garage = array("119", "120");
$terreni = array("121", "122");
$cantine = array("123", "124");
$tuttenoterreni = array($appartamenti, $case, $casevacanze, $uffici, $garage, $cantine);
?>

and i have this if condition:

<?php if ( osc_item_category_id() == $terreni) { ?>
<?php echo $custom_field_value['dimensioni-terreni'] ;?> mq
<?php } else if ( osc_item_category_id() == $tuttenoterreni) { ?>
<?php echo $custom_field_value['dimensioni'] ;?> mq
<?php } else { ?>
<?php } ?>

osc_item_category_id() is a number value

but not work. i don't understand where is problem...

6
  • osc_item_category_id() is returning what? Commented May 26, 2017 at 12:10
  • is osc_item_category_id() returning array or just a simple type ? and why you are comparing an array with == ? Commented May 26, 2017 at 12:11
  • what returning osc_item_category_id() ?? Commented May 26, 2017 at 12:12
  • osc_ite_category_id() return a number like 97 ore 98 or 99 or 100.... Commented May 26, 2017 at 12:16
  • In that case try if(in_array(osc_item_category_id(), $YourArray)){ .... Commented May 26, 2017 at 12:18

5 Answers 5

2

$terreni is single dimensional array and $tuttenoterreni is multi dimensional array.

For a single dimensional array, use in_array() function and for multi dimesnional array, create a custom function to find values in this multi dimensional array.

I've provided you the following code, which will help you to find values in multi dimensional array. Follow in_array() and multidimensional array

function in_array_r($needle, $haystack, $strict = false) {
  foreach ($haystack as $item) {
    if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
      return true;
    }
  }
  return false;
}

Code:

<?php 

function in_array_r($needle, $haystack, $strict = false) {
  foreach ($haystack as $item) {
    if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
      return true;
    }
  }
  return false;
}

if (in_array(osc_item_category_id(),$terreni)) {
  echo $custom_field_value['dimensioni-terreni'] ;
} elseif(in_array_r(osc_item_category_id(), $tuttenoterreni)) {
  echo $custom_field_value['dimensioni'] ;
} else {
  echo "Oops.!! No results found.";
}?>

Useful Links:

  1. in_array() - PHP Manual
  2. in_array() and multidimensional array
Sign up to request clarification or add additional context in comments.

Comments

1

You can't check "directly" this. You are trying to compare two type of variables.

An PHP array is a pointer to "multiple variables".

If I read your code correctly, probably your function osc_item_category_id returns an integer. In that case, the first if will change to:

<?php if (in_array(osc_item_category_id(), $terreni)) { ?>

You can check documentation about in_array function here: https://www.php.net/manual/en/function.in-array.php.

The elseif, have a similar problem. You've created a multidimensional array (an array of arrays). You need to use on this place the array_merge function (check documentation here: https://www.php.net/manual/en/function.array-merge.php), to create a unique array with all values of the another ones. Then, you can check as on the first example:

$tuttenoterreni = array_merge($appartamenti, $case, $casevacanze, $uffici, $garage, $cantine);

<?php } else if (in_array(osc_item_category_id(), $tuttenoterreni)) { ?>

1 Comment

try to store array values without "", to make them integer instead of strings.
0

if osc_item_category_id() function return no. 121,122 user this function to compare.

<?php 
    $in_id = osc_item_category_id();
    if(in_array($in_id,$terreni)){
       echo $custom_field_value['dimensioni-terreni'];
    }
?>

Comments

0

The in_array() function searches an array for a specific value.

Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.

Syntax: in_array(search,array,type)

Example :

$people = array("Peter", "Joe", "Glenn", "Cleveland");
if (in_array("Glenn", $people)){
  echo "Match found";
}

Comments

0

Use the function bool in_array ( value , array )

this function returns true is the value is present in the array

So modify the content of if and else if condition in your code, e.g:

if(in_array(osc_item_category_id() , $terreni ))

Also what I notice in your code is that in the elseif part you are trying to compare a numerical value returned by osc_item_category_id() with the value in the 'array of array', whereas in 'if' condition you are comparing the value returned by osc_item_category_id() with value in 'array'.

If it is the fact then in elseif part you need to run a foreach loop, comparing the value returned by 'osc_item_category_id()' with each array using the above 'in_array()' method.

Hope this help out!!!

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.