rgbflawed
rgbflawed

Reputation: 2137

Imploding and matching array with another array

Say I've got one array like this

$users_names=array(1=>"Abby",2=>"Betty",3=>"Cathy",4=>"Debby");

And another like this

$users_admin=array(1,3);

What is the best way to implode the $users_admin array matching to $users_names?

For example, I would want to do something like this:

echo implode(", ",magical_array_function($users_admin,$users_names));

//echos: "Abby, Cathy"

What I've been doing is this...

foreach ($users_admin as $id_user) $toEcho.=$users_names[$id_user].", ";
echo substr($toEcho,0,-2);

But I know there must be a more efficient way to do it in one line

Upvotes: 0

Views: 40

Answers (1)

gen_Eric
gen_Eric

Reputation: 227270

You're looking for array_intersect_key here.

array_intersect_key($users_names, array_flip($users_admin))

Upvotes: 3

Related Questions