You could just create a function class and join the class to NPCs you wish to use it in.
Also, it might be a bit nicer to use the built-in for () iterator.
PHP Code:
function find(arr, attr, val)
{
for (key: arr)
{
if (key.(@attr) == val)
{
return key;
}
}
return -1;
}