如何从数据库创建数组?

时间:2010-04-30 17:57:53

标签: php mysql arrays

请帮我从我的数据库字段创建一个数组。该字段的记录以逗号分隔。以下是插图:

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            |

我想收集 article_tags_fld 中的所有记录,然后将其放入1个数组中。也许我把它命名为 $ array1 ,打印出如下:

Array
(
[0] => PHP
[1] => coding
[2] => scripting
[3] => Javascript
[4] => scripting
[5] => tutorial
[6] => CSS
[7] => tutorial
[8] => web design
)

4 个答案:

答案 0 :(得分:8)

$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将用分隔符拆分字符串。

array_merge将合并两个数组。

array_map会对所有元素应用修剪。

trim会删除代码两边的任何空白区域。

答案 1 :(得分:7)

实际上,我会先将表格normalize分成多个表格。

制品

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

标签

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      |

然后在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;
}

当然,如果您只想要一个没有重复的标签列表,那么您可以使用此查询。

SELECT tag_title_fld
FROM tags

如果你想要他们计算使用频率的次数:

SELECT tag_title_fld, COUNT(*) AS tag_count
FROM tags
JOIN articles_tags USING(tag_ID)
GROUP BY tag_title_fld

答案 2 :(得分:2)

<强>已更新

您也可以使用 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];
}

<强>增加: 在一行:

// 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)))); 

显示:

 print_r( $array1 );

Array
(
    [0] => PHP
    [1] => coding
    [2] => scripting
    [3] => Javascript
    [4] => scripting
    [5] => tutorial
    [6] => CSS
    [7] => tutorial
    [8] => web design
)

答案 3 :(得分:1)

这应输出所有内容。

$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.