Jump to content

Problem with arrays and multiple values per key


Epimetheus1980

Recommended Posts

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! 

Link to comment
Share on other sites

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>";
	}

Link to comment
Share on other sites

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.

  • Like 2
Link to comment
Share on other sites

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);
}
?>
Link to comment
Share on other sites

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      |
                    +---------+-------+----------+------------+
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.