adjoke
adjoke

Reputation: 149

how to use php array_search to match 2 key value in multi dimensional array

I have a multi dimensional array and i want to use php array_search to find the key for where 2 key values match. below is my array.

    $array[] = [
        'id' => 2,
        'title' => 'product 2',
        'size' => 2
    ];

    $array[] = [
        'id' => 2,
        'title' => 'product 2',
        'size' => 1
    ];
$key = array_search(2, array_column($array, 'id'));

In the above case i get a $key value of 0 but the key value i want to get is 1. if is possible i want to use array_search to find where id=>2 and size=>1 in $array. Any help will be appreciated thanks.

Upvotes: 0

Views: 326

Answers (1)

sietse85
sietse85

Reputation: 1506

Why not use foreach?

foreach ($array as $row) {
    if ($row['id'] === 2 && $row['size'] === 1) {
         //found
    }
}

Upvotes: 3

Related Questions