I am building a phonebook application that will generate a list of extensions from our phone system. The phone system has two elements; "name" and "extension". I would like to separate the name into first name and last name. I have already done this with the following:
list($ln,$fn) = explode(' ',$contact[1],2);
The problem is, I would like to add $ln and $fn to the array that is formed from the database query. The reason for this is because I am creating a json from the array. I have tried the following, but it does not seem to add the fname and lname element to the array:
$contact_array = array();
while ($contact=mysql_fetch_array($QUERYresult))
{
if(preg_match('/\s/',$contact[1]))
{
//$count++;
$contact_array[] = $contact;
list($ln,$fn) = explode(' ',$contact[1],2);
$contact['fname'] = $fn;
$contact['lname'] = $ln;
}
fwrite($myJSON, json_encode($contact_array));
The output of this array only returns the database query, which looks like this:
Array
(
[0] => Array
(
[extension] => 1000
[name] => Extension 1
)
[1] => Array
(
[extension] => 1001
[name] => Extension 2
)
)
mysql_DB library. It was discontinued and deprecated years ago, and removed entirely in PHP7, mostly due to security issues. Switch to usingmysqliorPDOasap.$contactis added to$contact_arraywhen you do$contact_array[] = $contact;. It's not added by reference.