[eluser]obiron2[/eluser]
Guys,
I am struggling with the PHP logic to decode the following
I have an array that looks like this:
Code:
$contacts = Array
(
[Phone] => Array
(
[0] => Array
(
[userID] => 13
[contactType] => 1
[contactValue] => 999999999
[isPrimary] => 1
)
)
[Email] => Array
(
[0] => Array
(
[userID] => 13
[contactType] => 2
[contactValue] => [email protected]
[isPrimary] => 0
)
[1] => Array
(
[userID] => 13
[contactType] => 2
[contactValue] => [email protected]
[isPrimary] => 1
)
)
)
I want to pull the first key (phone, email) from the arrays to use as headings in the view.
The view data will look like this
Quote:<h1>Phone</h1>
999999999 Yes
<h1>Email</h1>
[email protected] No
[email protected] Yes
I can get the different records to group together by doing foreach($contacts as $group) and then foreach($group as $record)
What I can't seem to get is the top level keys as values.
The data comes from a single table of contacts, and is built by appending a results set for each contact type as an array to the contacts array.
Is there a better approach for marshalling the data into an associative array
Code for the contacts model and controller is below
Code:
model:
function getContactTypes()
{
$this->load->database();
$query = "SELECT Description from contact_type ORDER by sort_order";
return $this->db->query($query);
}
function getContactsByType($username,$type)
{
$this->load->database();
// get the userID for the user
$userID = $this->get_byUserName($username);
// get the typeID for the type
$typeID = $this->getContactTypeByDescription($type['Description']);
// run the main query
$query = "SELECT * from user_contacts where userID = $userID->ID AND contactType = $typeID->ID";
return $this->db->query($query);
}
Controller:
function contacts()
{
/**********************************************************************************
used to view and edit the contact methods for the signed in user
***********************************************************************************/
$this->load->model('users_model');
$username = $this->session->userdata['username'];
// build a multidimensional array of the contacts
$types = $this->users_model->getContactTypes()->result_array();
$contacts = array();
foreach($types as $type)
{
$contacts[$type['Description']] = $this->users_model->getContactsByType($username,$type)->result_array();
}
echo "<pre>";
print_r($contacts);
echo "</pre>";
}