I'd let PHP do some of the work JSON-wise:
<?php
// test data:
$members = array(
array(
'department' => 'good',
'firstname' => 'John',
'title' => 'Mr.',
'email' => 'foo@bar.com'
),
array(
'department' => 'bad',
'firstname' => 'Jack',
'title' => 'Dr.',
'email' => 'bar@foo.com'
),
array(
'department' => 'good',
'firstname' => 'Jane',
'title' => 'Ms.',
'email' => 'foo@bar.net'
)
);
$depttts = 'good';
?>
<script type='text/javascript'>
<?php
foreach($members as $memb)
{
if ($memb['department'] == $depttts){
$data = array(
"NAME" => $memb['firstname'],
"TITLE" => $memb['title'],
"CONTACT" => $memb['email'],
"RESEARCH" => "Ph.D."
);
$json = json_encode($data);
echo "contacts.push($json);\n";
}
}
?>
</script>
Output:
<script type='text/javascript'>
contacts.push({"NAME":"John","TITLE":"Mr.","CONTACT":"foo@bar.com","RESEARCH":"Ph.D."});
contacts.push({"NAME":"Jane","TITLE":"Ms.","CONTACT":"foo@bar.net","RESEARCH":"Ph.D."});
</script>