如何检查数组键值是否有元素

How to check if an array key value has an element

我有一个这样的数组

Array
(
    [0] => 
    [
        "18.06.2016",
        "18.06.2016",
        "18.06.2016",
        "Test Test",
        "Test Name",
        "Michael Dean",
        "London",
        "1",
        "980.00",
        "",
        "",
        "875.00",
        "875.00",
        "0",
        "64.81",
        "0",
        "810.19"
    ]
    [1] => 
    [
        "18.06.2016",
        "18.06.2016",
        "18.06.2016",
        "Tray 1",
        "Test Name",
        "Adam Richards",
        "London",
        "1",
        "980.00",
        "",
        "",
        "105.00",
        "105.00",
        "0",
        "7.78",
        "0",
        "97.22"
    ]...

我想检查数组键值是否在 London 之后为 1?

与第一个数组键一样,值的顺序如下:

...,"Test Name","Michael Dean","London","1",...

我该怎么做?

如果顺序始终相同,您可以循环遍历。

foreach ($array as $key => $value) {
    if ($value[4] == 'London' && $value[5] == 1) {
        echo '$array['.$key.'] has London and 1 as key 4 and 5';
    }
}

虽然您没有为子数组指定键,但它们仍然有默认键,您可以使用它们来获取您想要的值。

编辑

查看您的数组,我发现键的顺序没有逻辑。除非您将 town, id, date, name 等键分配给它们,否则您将无法找到您要查找的值。

例如:

array(
    '0' => array(
        'name' => 'John Doe',
        'town' => 'London',
        'active' => 1,
    )

);

使用常规 foreach 循环检查 London 值是否后跟 1:

// $arr is your initial array
foreach ($arr as $k => $v) {
    if ($v[6] == 'London') {
        $hasValue = ($v[7] != 1)? "NOT" : "";
        echo "Item with key $k has 'London' which is $hasValue followed by 1". PHP_EOL;
    }
}

示例输出:

Item with key 0 has 'London' which is  followed by 1
Item with key 1 has 'London' which is  followed by 1
...