Some background. I have two MySQL tables derived from PubMed XML files. The pubauth table has all authors for a given reference and the pubmed table has all the unique pubmed info. There are n+1 authors for each pubmed record. What I think I need is this:
Array(
[id]=> Array([id]=>1
[PMID]=>123456
Array(
[pubauth]=>Array(
[pubauth_id]=>1
[LastName]=>Chen
)
)
)
)
My model is:
$this->db->select( 'pubmed.id, PMID, pubauth.id AS pubauth_id, LastName');
$this->db->from( 'pubmed' );
$this->db->join( 'pubauth', 'pubmed_id = pubmed.id', 'left outer' );
$this->db->where( 'idecm',$keyword );
$query = $this->db->get();
$temp_result = array();
foreach ( $query->result_array() as $row ){
$temp_result[] = array(
'pubmed_id' => $row['id'],
'PMID' => $row['PMID'],
'LastName' => $row['LastName'],
'pubauth_id'=> $row['pubauth_id']
);
}
This returns each row as an array in $temp_result. What I am shooting for is similar to this question: PHP MYSQL multidimensional array. replacing the foreach in my Model according to the answer, I have:
foreach ( $query->result_array() as $row )
{
$r_array[$row["id"]]=array();
$row_st = (string) $row["pubauth_id"];
$r_array[$row["id"]][] = $row_st;
}
foreach ($r_array as $id => & $data) {
// Start with just ID
$newarray = array(
"id" => $id
);
if (count($data) == TRUE)
$newarray["pubauth_id"] = & $data;
$finalarray[] = & $newarray;
print_r( $finalarray);
unset($newarray);
}
This array I get from this is
Array ( [0] => Array ( [id] => 1 [pubauth_id] => Array ( [0] => 7 ) ) ) Array ( [0] => Array ( [id] => 1 [pubauth_id] => Array ( [0] => 7 ) ) [1] => Array ( [id] => 2 [pubauth_id] => Array ( [0] => 17 ) ) )
Data it is only the last row and LastName is missing. Sorry for rambling but this has me flumoxed. I can work with 2D arrays but cannot seem to wrap my head around this one. The output I am shooting for in the View would look like a PubMed reference; Author1...Author_n, Title, Data, Journal, Volume, Issue, Page. All I can get now is a Title, Data, Journal, Volume, Issue, Page for each Author as a row. Any help will be greatly appreciated.