2

Imagine I have this array:

Array
(
    [0] => Array
        (
            [email] => a@a.com
            [name] => a
        )

    [1] => Array
        (
            [email] => b@b.com
            [name] => b
        )
)

I use this code to check if my email exists in this multi array:

in_array($user->user_email, array_column($array, 'email'))

Now, my question is: How can I get the value of the parameter 'name', where the email is matching my variable. So if my $user->user_email is equal to 'a@a.com' i need the name value, which is 'a'. Is it possible in php?

jens_vdp
  • 594
  • 1
  • 4
  • 18

6 Answers6

5

Try this:

$index = array_search($user->user_email, array_column($array, 'email'));
if ($index !== false) $name = $array[$index]['name'];

This relies on the fact that the runtime array created by array_column preserves, I believe, the order in which the items were extracted. Ergo, an index read from this array can be used to reference the original array.

Mitya
  • 33,629
  • 9
  • 60
  • 107
3

Instead of getting column and then searching that array for your email. You can do it like this. array_column second parameter defines key of resultant array. This method reduce the effort of search data in an array.

Try this code snippet here

$user_email="a@a.com";

$result=array_column($array,"name","email");

if(isset($result[$user_email])){
    echo "Name is: ".$result[$user_email];//Name is: a
}

If you do print_r($result) You will get.

Array
(
    [a@a.com] => a
    [b@b.com] => b
)
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
1

The goal of this task in terms of efficiency is to perform the array search with a minimum number of iterations and to guarantee that the array is never possibly iterated more that one full time (that's a big O of n).

Any answers on this page that are preparing the data with array_column() (or a classic loop) before even beginning the search have already disqualified itself from being an efficient technique.

The most performant technique will be to iterate the array with the intention of using an early break. array_search() uses an early break under the hood, but unfortunately it needs the data structure to be adjusted first.

Using any functional iterators will not afford the use of an early break, so they can be discounted as well.

This is one time when a classic loop is most appropriate. With the conditional break, the snippet never iterates more than necessary to find the match.

Code: (Demo)

$array = [
    ['email' => 'a@a.com', 'name' => 'a'],
    ['email' => 'b@b.com', 'name' => 'b'],
];

$needle = 'a@a.com';

$name = null;
foreach ($array as $row) {
    if ($row['email'] === $needle) {
        $name = $row['name'];
        break;
    }
}
var_export($name);

Output:

'a'
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0
$name = "";

foreach($array as $key => $value)
{
    if($value["email"] == $user->user_email)
    {
        $name = $array[$key]["name"];
    }

}
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
Hrky
  • 1
  • 1
  • 2
    `$name = $value['name'];` is faster and more clear. You can also put a `break;` after it. – axiac Aug 30 '17 at 10:08
0

Use array_search insted of in_array

$array = [
    [
        'email' => 'a@a.com',
        'name' => 'a'
    ], [
        'email' => 'b@b.com',
        'name' => 'b'
    ]
];

$index = array_search('b@bc.com', array_column($array, 'email'));

// when there is no such value, $index will be equal 'false'
var_dump($index); // int(1)
0

Try this;

$name = $array[array_keys(array_column($array, 'email'), $user->user_email)[0]]['name'];

PD: (this code only get first element of search)

O.Palm
  • 162
  • 1
  • 3
  • 15