4

I want to search a key in a multidimensional array with two values in the condition.

I know how to search a multi-dimensional array with a single search condition:

$key = array_search($journee, array_column($data,'journee'));

but not more than that. Here is my array setup:

Array
(
    [0] => Array
        (
            [pseudo] => titi
            [journee] => 11
            [pts] => 3
        )

    ...
    [10] => Array
        (
            [pseudo] => test
            [journee] => 10
            [pts] => 6
        )

    [11] => Array
        (
            [pseudo] => test
            [journee] => 11
            [pts] => 4
        )

)

If I only put 11 in array_search and for array_column the key journee, it will return 0.

I want to add pseudo in the search condition too (the keys journee and pseudo should be searched for a specific values).

How would I accomplish this?

2
  • With one simple function it's not possible. With combination - it is. Commented Nov 4, 2016 at 11:49
  • so you want to search in json using both condition to be matched, journee and pseudo? Commented Nov 4, 2016 at 11:52

2 Answers 2

8

With one simple function it's not possible.

Here's a solution with two:

$search = ['pseudo' => 'test', 'journee' => 10];
$keys = array_keys(
    array_filter(
        $array,
        function ($v) use ($search) { return $v['pseudo'] == $search['pseudo'] && $v['journee'] == $search['journee']; }
    )
);
$key = $keys[0];

But if you need to find one key only I advise to use foreach & break, because you don't have to iterate over all array of values (what will happen with using array_filter) and stop immediately when certain data is found:

$key = false;
$search = ['pseudo' => 'test', 'journee' => 10];
foreach ($array as $k => $v) {
    if ($v['pseudo'] == $search['pseudo'] && $v['journee'] == $search['journee']) {
        $key = $k;
        // key found - break the loop
        break;
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Even if he doesn't need to break after the first match, a simple loop is better. So the answer is reduced to use a simple loop =)
Explain why simple loop is better.
1) loop is more readable, 2) loop is a single construct, while the former solution uses a lambda and two function calls, all joined into a complicated expression; thus, loop should be more productive
Thanks, now OP can decide what to use)
Typo here most probably: $return ?
|
0

To enjoy the efficiency of a conditionally halted loop while excluding the temporary variables from the global scope, use an Immediately Invoked Functional Expression. The qualifying row can be identified using array_diff_assoc() to check that all associative elements in the search array where matched in the given row. Demo

$search = ['pseudo' => 'test', 'journee' => 10];

var_export(
    (function($haystack, $needle) {
        foreach ($haystack as $i => $row) {
            if (!array_diff_assoc($needle, $row)) {
                return $i;
            }
        }
        return null;
    })($array, $search)
);
// output: 10

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.