Search code examples
phparraysarray-key

How to check if an array key value has an element


I have an array like this

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"
    ]...

I want to check if the array key value has 1, after London or not?

Like in the first array key, the value goes like this order:

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

How can I do that?


Solution

  • If the order is always the same you can just loop through.

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

    Though you have not appointed keys to the sub-arrays, they still have default keys and you can use these to get to the values you want.

    Edit

    Taking a look at your array I see that there is no logic to the order of the keys. You will not be able to find the values you are looking for unless you assign keys such as town, id, date, name to them.

    For example:

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

    );