PHP : Check if an array contains any value in a set

In this tutorial Learn How to Check if an array contains any value in a set in php using in_array() method


function array_contains_any_val(array $array, array $subset, bool $strict = false) :bool
{
    $contains = false;
    foreach ($subset as $value) {
        if (in_array($value, $array, $strict)) {
            $contains = true;
            break;
        }
    }
    return $contains;
}



TUTORIAL

<?php
/**
 * Check if an array contains any value in a set.
 **
 * @param array $array
 * @param array $subset
 * @param bool  $strict  Strict type checking
 * @return bool
 */
$a=array(1,'test');
$b=array(2,'test');

function array_contains_any_val(array $array, array $subset, bool $strict = false) :bool
{
    $contains = false;
    foreach ($subset as $value) {
        if (in_array($value, $array, $strict)) {
            $contains = true;
            break;
        }
    }
    return $contains;
}

var_dump(array_contains_any_val($a, $b));


OUTPUT DEMO

bool(true)

No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...