简体   繁体   中英

How to check if value is equal to string in array

I want to check if the value: diam-mm exist in array, if the value not exist do something.

A array can have multiple properties, property name is: [PropertyType]->[Name]

i thought i loop to the properties and check if diam-mm value exist, else do something but because of the loop he does import the value mutliple times instead of one time.

Example of one array with properties:

[2] => Array
        (
            [PropertyType] => Array
                (
                    [Guid] =>
                    [DataType] => Text
                    [Name] => diam-mm
                    [Unit] => 
                )

            [BooleanValue] => 
            [DateTimeValue] => 
            [NumericValue] => 
            [TextValue] => 400
            [XmlValue] => 
            [UrlValue] => 400
        )

    [3] => Array
        (
            [PropertyType] => Array
                (
                    [Guid] => 
                    [DataType] => Text
                    [Name] => lengte-mm
                    [Unit] => 
                )

            [BooleanValue] => 
            [DateTimeValue] => 
            [NumericValue] => 
            [TextValue] => 2000
            [XmlValue] => 
            [UrlValue] => 2000
        )
<?php 
   for ($i=0; $i <count($array) ; $i++) { 
     if($array[$i]['PropertyType']['Name']=="diam-mm"){
        // your code
     }
   }
?>

If you want to check if an array key matches a value you can do so using simple variable assignment. However you would need to loop through each array index item and enumarate the loop based on it's iteration.

To create the loop I would suggest using count to count the amount of items in the array. We will assign the result to a variable:

$count = count($my_array);

Do keep in mind that count only counts the number of items based on their actual count, not their array index. This means that an array with indexes starting from zero which has an index of 0-30 would return 31 as the result of count because count counted the zero index as an actual count value.

To fix this we need to subtract 1 from the result of count :

$count = $count - 1;

Then we can use the count as the number of repeats in a for loop. Where the variable $i represents the iteration that the loop is going through:

//Loop through each array index
for($i=0; $i <= $count; $i++){
    //Assign the value of the array key to a variable
    $value = $my_array[$i]['PropertyType']['Name'];
    //Check if result string contains diam-mm
    if(str_contains($value, 'diam-mm'){
        echo 'The value matches!';
    } else{
        echo 'The value does not match!';
    }
}

Try this function, i hope this answer your question...

function array_recursive_search_key_map($needle, $haystack) {
    foreach($haystack as $first_level_key=>$value) {
        if ($needle === $value) {
            return array($first_level_key);
        } elseif (is_array($value)) {
            $callback = $this->array_recursive_search_key_map($needle, $value);
            if ($callback) {
                return array_merge(array($first_level_key), $callback);
            }
        }
    }
    return false;
}

How to use

$yourValue = "diam-mm";
$array_keymap = array_recursive_search_key_map($yourValue, $yourArray);
var_dump($array_keymap);

Output

    Array
(
    [0] => 0
    [1] => PropertyType
    [2] => Name
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM