1

I have a table that is listed like this:

Table name: test

|--------------------|
|------Column 1------|
|--------------------|
|John,Raul,Matt,Tyler|
|Tim,Hannah----------|
|Zeke,Brady,Tim,Tyler|
|Elliot,Zeke---------|
|Matt,Andrew,Idda----|
...

And I need to get all of those names into a PHP array, without multiple cases of the same name.

Would I be better off getting each unique name via SQL, or should I just pull the column, and then parse through it via PHP?

The final PHP array would look like:

$test_array[0]="John"
$test_array[1]="Raul"
$test_array[2]="Matt"
$test_array[3]="Tyler"
$test_array[4]="Tim"
$test_array[5]="Hannah"
$test_array[6]="Zeke"
$test_array[7]="Brady"
$test_array[8]="Elliot"
$test_array[9]="Andrew"
$test_array[10]="Idda"

I'm not sure how I would parse each cell in the table. What would you guys do?

john
  • 139
  • 1
  • 8

1 Answers1

0

I agree it's best to normalize the database. However, given the situation you've described, I would perform the SQL select and use PHP to run through the results.

I'd initialize $results as an empty string:

$results = '';

Then I'd concatenate each row with $results:

$results .= $row;

Then I'd use explode() to put all of the names in an array:

$all_names = explode(',', $results);

Then I'd use array_unique() to reduces the $all_names array to unique values:

$unique_names = array_unique($all_names);

HTH.

Jeff Cohan
  • 156
  • 6