简体   繁体   English

检查多维数组中的键值对

[英]Check for key-value pair in multidimensional array

I have the follow array: 我有以下数组:

Array
(
    [0] => Array
        (
            [type] => foo
        )

    [1] => Array
        (
            [type] => bar
        )

    [2] => Array
        (
            [type] => bar
        )
)

and need to know if exists one or more type which value is bar , without do this: 并且需要知道是否存在一个或多个type哪个值为bar ,而不执行此操作:

foreach ($arrayOfTypes as $type) {
    if ($type['type'] == 'bar')
    {
        // Stuff here
    }
}

(Only for learning purposes) (仅用于学习目的)

I'd go with array_filter() ; 我会选择array_filter() ;

$filteredArray = array_filter($stuff, function($item){

    return $item['type'] == 'bar';

});

if( count($filteredArray) > 0 ){

    echo 'There was a bar item in array.';

}else{

    echo 'No luck sorry!';

}

Use in_array: http://se.php.net/manual/en/function.in-array.php 使用in_array: http ://se.php.net/manual/en/function.in-array.php

Combine it with array_map to flatten what you have. 将它与array_map结合使用可以展平你拥有的东西。 Like so: 像这样:

$new_array = array_map( function( $arr ) {
    return $arr['type'];
}, $array );
in_array( 'bar', $new_array );

Honestly a foreach loop or moonwave99's array-filter answer is probably going to be your best bet, but if what you're looking is the shortest code possible and creativity that will make most programmers gag, you could try serialize -ing the array and using string-searching functions: 老实说,一个foreach循环或moonwave99的array-filter答案可能是你最好的选择,但如果你正在寻找的是最短的代码和创造力,这将使大多数程序员窒息,你可以尝试serialize阵列和使用字符串搜索功能:

serialize(array(
        0=>array(
            'type'  => 'foo'
        ),
        1=>array(
            'type'  => 'bar'
        ),
        2=>array(
            'type'  => 'bar'
        )
    ))

becomes

a:3:{i:0;a:1:{s:4:"type";s:3:"foo";}i:1;a:1:{s:4:"type";s:3:"bar";}i:2;a:1:{s:4:"type";s:3:"bar";}}

So you can now run a strpos() or preg_match() function to find them. 所以你现在可以运行strpos()preg_match()函数来找到它们。 So your whole function would look like : 所以你的整个功能看起来像

$exists = strpos('{s:4:"type";s:3:"bar";}',serialize($arrayOfTypes)); //returns number or false

It's short, it's snappy, and get's the job done for simple string keypairs. 这很简短,很简单,并且完成了简单的字符串密钥对的工作。

This is basically the same as moonwave99's solution but slightly more useful as it's in a function that you can feed a key/value pair to as well, so it can be used to search for any key/value combo: 这与moonwave99的解决方案基本相同,但稍微更有用,因为它在一个函数中也可以提供一个键/值对,因此它可以用于搜索任何键/值组合:

function isKVInArray($k, $v, $array) {
    $filtered = array_filter($array, function($item) use($k,$v) {
        return $item[$k] == $v;
    });
    if(count($filtered)>=1) return true;
    else return false;
}

$k = 'live';
$v = 'y';
$my_array = array(
    0=>array(
        'live'=>'n',
        'something_else'=>'a'
    ),
    1=>array(
        'live'=>'y',
        'something_else'=>'b'
    ),
    2=>array(
        'live'=>'n',
        'something_else'=>'c'
    )
);


if(isKVInArray($k, $v, $my_array)) echo "$k=>$v was found in the array.";
else echo 'No luck sorry!';

there's another simple way which is useful when you need to count all the different values 当你需要计算所有不同的值时,还有另一种简单的方法

foreach ($arrayOfTypes as $type) $cnt[$type['type']]++;

To get the number of 'bar' (or to get the count of another value): 获取'bar'的数量(或获取另一个值的计数):

echo($cnt['bar']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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