简体   繁体   中英

search a value in multi dimensional array that has multiple same key names

imagine that you have this array

Array(
[0] => Array
    (
        ['id'] => 12,
        ['filter'] => Array
            (
                ['resource'] => Array
                    (
                        ['id'] => 44444
                    )

            ),

        ['filtervalue'] => Array
            (
                ['resource'] => Array
                    (
                        ['id'] => 12345
                    )

            )

    ),
[1] => Array
    (
        ['id'] => 24,
        ['filter'] => Array
            (
                ['resource'] => Array
                    (
                        ['id'] => 44444
                    )

            ),

        ['filtervalue'] => Array
            (
                ['resource'] => Array
                    (
                        ['id'] => 12
                    )

            )

    ),
[2] => Array
    (
        ['id'] => 25,
        ['filter'] => Array
            (
                ['resource'] => Array
                    (
                        ['id'] => 55555
                    )

            ),

        ['filtervalue'] => Array
            (
                ['resource'] => Array
                    (
                        ['id'] => 123
                    )

            )

    ),
[3] => Array
    (
        ['id'] => 26,
        ['filter'] => Array
            (
                ['resource'] => Array
                    (
                        ['id'] => 55555
                    )

            ),

        ['filtervalue'] => Array
            (
                ['resource'] => Array
                    (
                        ['id'] => 12
                    )

            )

    )
);

all i want to know if these 2 values exist:

if filter->resource->id == 55555 AND if filtervalue->resource->id == 12 so in this array they exist in [3] so this is true

if I would've search for if filter->resource->id == 44444 AND if filtervalue->resource->id == 123

then it would give a false although 4444 exist in [0] and [1] and 123 exist in [2]

i am probobly overthinking the problem

You can use foreach loop to implement this and check your required condition and get its index:

$index = '';
foreach ($records as $key => $record) {
    if (($record['filter']['resource']['id'] == 55555) && ($record['filtervalue']['resource']['id'] == 12)) {
        $index = $key;
        break;
    }
}
$found = false;
foreach ($yourArray as $item) {
    if ($item['filter']['resource']['id'] == $someVal && $item['filtervalue']['resource']['id'] == $anotherVal) {
        $found = true;
        break;
    }
}
var_dump($found);
$filters = []; // Your array
$r = 44444;
$v = 123;
foreach( $filters as $f ){
    if( $r === (int) $f ['filter']['resource']['id'] ){
      $r = true;
    }
    if( $v === (int) $f ['filtervalue']['resource']['id'] ){
      $v = true;
    }
    if( TRUE === $r && TRUE === $v ){
      echo "FOUND!!!\n";
      break;
    }
}

i am probobly overthinking the problem -- Absolutely!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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