Epimetheus1980 Posted January 13, 2015 Share Posted January 13, 2015 I am very grateful for any input. I have a field in a database that contains references (pattern: authorYYYY), such as the following string: "authorA1989; authorB1978a; authorB1984; authorC1999; authorC2000a; authorC2000b; authorC2002; authorD2009" What I want is an output as the following in order to create a proper bibliography (therefore accessing another table and picking out the respective reference details): authorA 1989 authorB 1978a 1984 authorC 1999 2000a 2000b 2002 authorD 2009 I somewhat fail to group the values (years) per author. I know that keys are unique, but is there a way to work around this? Many thanks for any help! Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted January 13, 2015 Share Posted January 13, 2015 You could create an associative array where "authorA", "authorB", etc. are the keys. Then just add the years using code similar to this: $authorArray['authorA'][] = 1989; Quote Link to comment Share on other sites More sharing options...
Epimetheus1980 Posted January 13, 2015 Author Share Posted January 13, 2015 Thank you. Although this makes sense to me, I am not sure about filling the array. Here is my current code. I do not know how to implement the associative array with the year-values. $arr = array(); foreach(explode(';', $string) as $pair) { $author = preg_replace("/([A-Za-z]*)([0-9]*[a-z]?)/", "$1", $pair); $year = preg_replace("/([A-Za-z]*)([0-9]*[a-z]?)/", "$2", $pair); $arr[$author] = null; } foreach($arr as $key=>$val) { echo "<p>" . $key . "</p>"; } Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 13, 2015 Share Posted January 13, 2015 Backing up for a bit. Your first post indicates you have a db field that contains multiple values. You might solve your problem if you simply followed the rules of a properly structure database. Normalization (look it up) means you never put multiple discrete values into one field, but rather put them into separate records of a separate table. In your case that means that this: [field1] authorA1989; authorB1978a; authorB1984; authorC1999; authorC2000a; authorC2000b; authorC2002; authorD2009 being in one field of a table would become multiple records in a separate table: [field1] authorA1989 authorB1978a authorB1984 authorC1999 authorC2000a authorC2000b authorC2002 authorD2009 where this table is linked back to the first table by some id value in the first table. You really need to understand this and implement it. 2 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted January 13, 2015 Share Posted January 13, 2015 Thank you. Although this makes sense to me, I am not sure about filling the array. Here is my current code. I do not know how to implement the associative array with the year-values. You could try something like this (note that the code is untested): <?php $arr = array(); foreach(explode(';', $string) as $pair) { $author = preg_replace("/([A-Za-z]*)([0-9]*[a-z]?)/", "$1", $pair); $year = preg_replace("/([A-Za-z]*)([0-9]*[a-z]?)/", "$2", $pair); //IF THIS IS THE FIRST ENTRY FOR THE CURRENT AUTHOR, INITIALIZE YEAR ARRAY if(!isset($arr[$author])) { $arr[$author] = array(); } //ADD CURRENT YEAR $arr[$author][] = $year; } foreach($arr as $key=>$val) { echo "<p>" . $key . "</p>"; echo implode('<br>', $val); } ?> Quote Link to comment Share on other sites More sharing options...
Epimetheus1980 Posted January 13, 2015 Author Share Posted January 13, 2015 (edited) Thank you. That's all already implemented. I just simplified it by using a string similar to the field in Table 1. My problem was that I didn't understand how to assign the values (years) to the respective keys (authors). Edited January 13, 2015 by Epimetheus1980 Quote Link to comment Share on other sites More sharing options...
Barand Posted January 13, 2015 Share Posted January 13, 2015 If you are going to normalize as ginerjm recommended then do it properly and not have multiple values in a single field. "AuthorA2000a" is 3 separate fields viz. | Name | Year | Sequence | You would store publications in separate table from the Author table and store the authorID as a foreign key author +----------+-------------------+ | authorID | name | +----------+-------------------+ | 1 | AuthorA | | 2 | AuthorB | +----------+-------------------+ | | +-------------------------------------+ | publication | +---------+-------+----------+------------+ | pubID | year | authorID | pubname | +---------+-------+----------+------------+ | 1 | 2000 | 1 | pub 1 | | 2 | 2000 | 1 | pub 2 | | 3 | 2001 | 1 | pub 3 | | 4 | 2000 | 2 | pub 4 | | 5 | 2001 | 2 | pub 5 | | 6 | 2002 | 2 | pub 6 | | 7 | 2002 | 2 | pub 7 | +---------+-------+----------+------------+ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.