Reputation: 17
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...
Upvotes: 0
Views: 6252
Reputation: 764
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!!!
Upvotes: 0
Reputation: 1719
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";
}
Upvotes: 0
Reputation: 110
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'];
}
?>
Upvotes: 0
Reputation: 1884
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)) { ?>
Upvotes: 1
Reputation: 10548
$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:
Upvotes: 2