tags:

views:

123

answers:

4

Hi,

Please help me to create an array from a field of my DB. That field has records separated by comma. Below is the illustration:

ID | article_title_fld         | article_tags_fld                     |
----------------------------------------------------------------------
1  | Learn PHP                 | PHP, coding, scripting               |
3  | Javascript Tutorial       | Javascript, scripting, tutorial      |
4  | Styling with CSS          | CSS, tutorial, web design            |

I want to collect all records in the article_tags_fld then put it into 1 array. Perhaps I named it $array1, and the print out as below:

Array
(
[0] => PHP
[1] => coding
[2] => scripting
[3] => Javascript
[4] => scripting
[5] => tutorial
[6] => CSS
[7] => tutorial
[8] => web design
)
+5  A: 
$array1 = array();
$result = mysql_query("SELECT article_tags_fld FROM MY_TABLE");
while ($row = mysql_fetch_assoc($result)) {
   $array1 = array_merge($array1, array_map('trim', explode(",", $row['article_tags_fld'])));
}

explode will split a string by a delimiter.

array_merge will combine two arrays.

array_map will apply trim to all elements.

trim will remove any white space on either side of your tags.

Geoff
I'd throw a `trim` into the mix as well.
Svish
good point, updating
Geoff
I tried this, i got error msg "Warning: Invalid argument supplied for foreach() in C:\wamp\www\test\untitled4.php on line 20Array ( )"
Sofyan
Changed to use a while, loop, try that.
Geoff
Perfect when Using while, loop. Thanks.
Sofyan
Good. Sorry about that. Be careful with `array_merge`. Now that I think about it, you may not get both copies of "tutorial" as you originally requested.
Geoff
+1  A: 

UPDATED

you can do also this way by using mysql_fetch_Array

$array1 = array();
$result = mysql_query("SELECT ID , article_tags_fld FROM my_table");

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
    $array1[] = $row['article_tags_fld'];
    // OR 
    // $array1[] = $row[1];
}

ADDED: in one line:

// use this version to behave as in your example..
$array1 = array_map('trim',explode(',',implode(',',$array1))); 

// use this version with array_unique for a non duplicate way...    
$array1 = array_unique(array_map('trim',explode(',',implode(',',$array1)))); 

DISPLAY:

 print_r( $array1 );

Array
(
    [0] => PHP
    [1] => coding
    [2] => scripting
    [3] => Javascript
    [4] => scripting
    [5] => tutorial
    [6] => CSS
    [7] => tutorial
    [8] => web design
)
aSeptik
The out put is "Array ( [0] => PHP, coding, scripting [1] => Javascript, scripting, tutorial [2] => CSS, tutorial, web design )"
Sofyan
Opps! sorry Sofyan! now i have understood what you want! my bad! let me change it!
aSeptik
ok updated! use **array_unique** for filter array! ;)
aSeptik
Output: Array ( [0] => PHP [1] => coding [2] => scripting Javascript [3] => scripting [4] => tutorial CSS [5] => tutorial [6] => web design )// There is should be a comma between "scripting" and "javascript" on key 2, between "tutorial" and "CSS" on key 4.
Sofyan
doh! ;-) updated one more time, this time i have tested it personally! ;-)
aSeptik
you missed the s1ngle quote on the left side "trim"
Sofyan
edited! ;-) ohhh my friend, i'm so freaky!!! ;-)
aSeptik
+4  A: 

Actually, I would normalize the table into multiple tables first.

articles

article_ID | article_title_fld         |
----------------------------------------
1          | Learn PHP                 |
3          | Javascript Tutorial       |
4          | Styling with CSS          |

tags

tag_ID | tag_title_fld |
------------------------
1      | PHP           |
2      | coding        |
3      | scripting     |
4      | Javascript    |
5      | tutorial      |
6      | CSS           |
7      | web design    |

article_tags

article_ID | tag_ID |
---------------------
1          | 1      |
1          | 2      |
1          | 3      |
3          | 4      |
3          | 3      |
3          | 5      |
4          | 6      |
4          | 5      |
4          | 7      |

and then in the PHP

$array1 = array();
$result = mysql_query("
SELECT tag_title_fld
FROM tags
JOIN articles_tags USING(tag_ID)
ORDER BY article.article_ID
");
foreach($result as $row) { 
   $array1[] = $row['tag_title_fld'];
   // With count. See below for the query it goes with
   // $array2 = array();
   // $array2['tag'] = $row['tag_title_fld'];
   // $array2['count'] = $row['tag_count'];
   // $array1[] = $array2;
}

Of course, if you just wanted a list of the tags without duplicates, you'd use this query instead.

SELECT tag_title_fld
FROM tags

and if you wanted them with a count of how often they're used:

SELECT tag_title_fld, COUNT(*) AS tag_count
FROM tags
JOIN articles_tags USING(tag_ID)
GROUP BY tag_title_fld
R. Bemrose
+1  A: 

This should output everything.

$pdo = new PDO( /* CONNECTION */ );
$stmt = $pdo->query( 'SELECT article_tags_fld FROM TABLENAME' );

function mapFunc( $row ){ return explode( ',', $row[ 0 ] ) }

$all = array();
foreach( array_map( 'mapFunc', $stmt->fetchAll() ) as $row )
{
   $all = array_merge( $all, $row );
}
// $all now holds all of the values.
Christopher W. Allen-Poole