i'm able to split a string into an array
我可以把一个字符串分割成一个数组
$array = preg_split("/[\s]*[,][\s]*/", $category);
RESULT
Array ( [0] => jquery[1] => bla[2] => bla);
but now i have a database with a row holding all by categories ("cat1, 2, 3, ..") - multiple records have similar categories- so in order to create a quick "category" menu i'm using this code this code:
但是现在我有了一个数据库,它包含了所有类别(“cat1, 2, 3, ..”)——多个记录有类似的类别,所以为了创建一个快速的“类别”菜单,我使用了这个代码:
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
$category[] = $row[category];
}
$array = array_unique($category);
as you can imagine the result is like this:
正如你可以想象的,结果是这样的:
Array ( [0] => bla1,bla2, bla3[1] => bla6, bla4[2] => bla11 ....);
阵列([0]= > bla1、bla2 bla3[1]= > bla6,bla4[2]= > bla11 ....);
is there a way to slip this array so that (one more time) i get a slip array
是否有一种方法可以使这个数组(再一次)得到一个slip数组
i have used the above "array_unique" method but is not working - though all google searches lead me to the same result (being the above one)
我使用了上面的“array_unique”方法,但是没有使用—尽管所有谷歌搜索都会导致相同的结果(如上所示)
hope you could help
希望你可以帮助
thanks
谢谢
1 个解决方案
#1
3
First of: Splitting a string is easier with the explode()
function.
首先:使用blow()函数拆分字符串更容易。
Guessing what you want to do i wrote up a little code sample:
猜猜你想做什么,我写了一个小代码示例:
<?php
$dbRows = array("x,y,z", "a,b,c", "y,b,c");
$allCats = array();
foreach($dbRows as $row) {
$allCats = array_merge($allCats, explode(",", $row));
}
var_dump(array_unique($allCats));
/*
array(6) {
[0]=> string(1) "x"
[1]=> string(1) "y"
[2]=> string(1) "z"
[3]=> string(1) "a"
[4]=> string(1) "b"
[5]=> string(1) "c"
}
*/
so you take out all of the values, fill a big array with those and filter it out at the end.
所以你取出所有的值,用它们填充一个大数组,最后过滤掉它。
I hope this is what you are trying to do.
我希望这是你想做的。
On a sidenote: Just in general it is not considered "good practice" to store stuff in your database that you need to perform string operations on. You might want to only store one value per column to make your live easier.
在旁注中:一般来说,将需要执行字符串操作的内容存储在数据库中并不被认为是“良好的实践”。您可能希望每个列只存储一个值,以使您的操作更简单。
( See this * question: What is Normalisation (or Normalization)? )
(请参阅*问题:什么是规范化(或规范化)?)
Edit
A shorter version of the above code with the same output:
上述代码的较短版本,输出相同:
<?php
$dbRows = array("x,y,z", "a,b,c", "y,b,c");
$allCats = explode(",", implode(",", $dbRows));
var_dump(array_unique($allCats));
this creates one big string ( "x,y,z,a,b,c,y,b,c" ) and splits that one. Left the above example as i'd guess it is easier to read/clearer
这将创建一个大字符串(“x,y,z,a,b,c,y,b,c”),并将其拆分。就像我猜想的那样,上面的例子更容易阅读/更清楚
#1
3
First of: Splitting a string is easier with the explode()
function.
首先:使用blow()函数拆分字符串更容易。
Guessing what you want to do i wrote up a little code sample:
猜猜你想做什么,我写了一个小代码示例:
<?php
$dbRows = array("x,y,z", "a,b,c", "y,b,c");
$allCats = array();
foreach($dbRows as $row) {
$allCats = array_merge($allCats, explode(",", $row));
}
var_dump(array_unique($allCats));
/*
array(6) {
[0]=> string(1) "x"
[1]=> string(1) "y"
[2]=> string(1) "z"
[3]=> string(1) "a"
[4]=> string(1) "b"
[5]=> string(1) "c"
}
*/
so you take out all of the values, fill a big array with those and filter it out at the end.
所以你取出所有的值,用它们填充一个大数组,最后过滤掉它。
I hope this is what you are trying to do.
我希望这是你想做的。
On a sidenote: Just in general it is not considered "good practice" to store stuff in your database that you need to perform string operations on. You might want to only store one value per column to make your live easier.
在旁注中:一般来说,将需要执行字符串操作的内容存储在数据库中并不被认为是“良好的实践”。您可能希望每个列只存储一个值,以使您的操作更简单。
( See this * question: What is Normalisation (or Normalization)? )
(请参阅*问题:什么是规范化(或规范化)?)
Edit
A shorter version of the above code with the same output:
上述代码的较短版本,输出相同:
<?php
$dbRows = array("x,y,z", "a,b,c", "y,b,c");
$allCats = explode(",", implode(",", $dbRows));
var_dump(array_unique($allCats));
this creates one big string ( "x,y,z,a,b,c,y,b,c" ) and splits that one. Left the above example as i'd guess it is easier to read/clearer
这将创建一个大字符串(“x,y,z,a,b,c,y,b,c”),并将其拆分。就像我猜想的那样,上面的例子更容易阅读/更清楚