PHP数组检查特定键中的值是否存在

时间:2022-10-26 16:51:15

I am using PHP 5.5.12.

我使用的是PHP 5.5.12。

I have the following multidimensional array:

我有以下多维数组:

[
    {
        "id": 1,
        "type":"elephant", 
        "title":"Title of elephant"
    }, 
    {
        "id": 2,
        "type":"tiger", 
        "title":"Title of tiger"
    },
    {
        "id": 3,
        "type":"lion", 
        "title":"Title of lion",
        "children":[{
            "id": 4,
            "type":"cow", 
            "title":"Title of cow"
        },
        {
            "type":"elephant", 
            "title":"Title of elephant"
        },
        {
            "type":"buffalo", 
            "title":"Title of buffalo"
        }]
    }
]

I am iterating this array using foreach loop.

我正在使用foreach循环迭代这个数组。

The array key type must be in elephant, tiger and lion. If not, then the result should return false.

阵列键类型必须是大象,虎和狮子。如果不是,那么结果应该返回false。

How can I achieve this?

我怎样才能做到这一点?

4 个解决方案

#1


Since you're using PHP5.5.12, you can make use of array_column.

由于您使用的是PHP5.5.12,因此可以使用array_column。

$arr = json_decode($json, true);
//Walk through each element, only paying attention to type
array_walk( array_column($arr, 'type'), function($element, $k) use(&$arr) {
    $arr[$k]['valid_type'] = in_array($element, array('lion', 'tiger', 'elephant'));
});

From here, each element in the array ($arr) will have a new key valid_type with a boolean value - 1 if the type is valid, 0 if it isn't.

从这里开始,数组中的每个元素($ arr)将有一个带有布尔值的新键valid_type - 如果类型有效则为1,如果不是则为0。

https://eval.in/350322

#2


So you want to check if your $myArray contains a value or not:

所以你想检查你的$ myArray是否包含一个值:

// first get all types as an array
$type = array_column($myArray, "type");

// specify allowed types values
$allowed_types = ["lion", "elephant", "tiger"];

$count = count($type);
$illegal = false;

// for loop is better
for($i = 0; $i < $count; $i++)
{
    // if current type value is not an element of allowed types
    // array, then both set the $illegal flag as true and break the 
    // loop
    if(!in_array($type[$i], $allowed_types)
        $illegal = true;
        break;
}

#3


Is this something that you are looking for?

这是你要找的东西吗?

foreach($your_array as $item) {
    if (!array_key_exists('type', $item)) {
    return FALSE;
    }
}

#4


function keyExists($arr, $key) {
    $flag = true;

    foreach($arr as $v) {
        if(!isset($v[$key])) {
            $flag = false;
            break;
        }
    }

    return $flag;
}

Hope this helps :)

希望这可以帮助 :)

#1


Since you're using PHP5.5.12, you can make use of array_column.

由于您使用的是PHP5.5.12,因此可以使用array_column。

$arr = json_decode($json, true);
//Walk through each element, only paying attention to type
array_walk( array_column($arr, 'type'), function($element, $k) use(&$arr) {
    $arr[$k]['valid_type'] = in_array($element, array('lion', 'tiger', 'elephant'));
});

From here, each element in the array ($arr) will have a new key valid_type with a boolean value - 1 if the type is valid, 0 if it isn't.

从这里开始,数组中的每个元素($ arr)将有一个带有布尔值的新键valid_type - 如果类型有效则为1,如果不是则为0。

https://eval.in/350322

#2


So you want to check if your $myArray contains a value or not:

所以你想检查你的$ myArray是否包含一个值:

// first get all types as an array
$type = array_column($myArray, "type");

// specify allowed types values
$allowed_types = ["lion", "elephant", "tiger"];

$count = count($type);
$illegal = false;

// for loop is better
for($i = 0; $i < $count; $i++)
{
    // if current type value is not an element of allowed types
    // array, then both set the $illegal flag as true and break the 
    // loop
    if(!in_array($type[$i], $allowed_types)
        $illegal = true;
        break;
}

#3


Is this something that you are looking for?

这是你要找的东西吗?

foreach($your_array as $item) {
    if (!array_key_exists('type', $item)) {
    return FALSE;
    }
}

#4


function keyExists($arr, $key) {
    $flag = true;

    foreach($arr as $v) {
        if(!isset($v[$key])) {
            $flag = false;
            break;
        }
    }

    return $flag;
}

Hope this helps :)

希望这可以帮助 :)