逗号分隔数组值 [英] split array values when there is a comma

查看:70
本文介绍了逗号分隔数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够将字符串拆分为数组

  $ array = preg_split( / [\s] * [,] [\s] * /,$ category); 

结果

数组([0] => jquery [1] => bla [2] => bla);

但是现在我有一个数据库,其中包含按类别( cat1、2、3, ..)-多个记录具有相似的类别-因此,为了创建快速的类别菜单,我使用以下代码:

  while($ row = mysql_fetch_array($ result,MYSQL_ASSOC))
{
$ category [] = $ row [category];
}
$ array = array_unique($ category);

您可以想象结果是这样的:



Array([0] => bla1,bla2,bla3 [1] => bla6,bla4 [2] => bla11 ....);



是否有一种方法可以滑动此数组,以便(再有一次)我可以得到滑动数组



我使用了上面的 array_unique方法,但无法正常工作-虽然所有的google搜索都会使我得到相同的结果(即上述结果)



希望您能提供帮助



谢谢

解决方案

首先: explode() 函数。



猜测您想要的内容我是否编写了一些代码示例:

 <?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
}
* /

取出所有值,并用这些值填充一个大数组,最后将其过滤掉。



我希望这是您要尝试的操作。






<旁注:通常,将需要在其上执行字符串操作的内容存储在数据库中不被视为好的做法。
您可能只想为每列存储一个值,以使您的生活更轻松。



(参见以下Stackoverflow问题:什么是标准化(或标准化)?



编辑



上述代码的较短版本,输出相同:

 <?php 
$ dbRows = array( x,y,z, a,b,c, y,b,c);
$ allCats = explode(,,implode(,,$ dbRows));
var_dump(array_unique($ allCats));

这会创建一个大字符串( x,y,z,a,b,c,y, b,c)并将其拆分。离开上面的示例,因为我想这样更容易阅读/清楚


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:

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

is there a way to slip this array so that (one more time) i get a slip array

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)

hope you could help

thanks

解决方案

First of: Splitting a string is easier with the explode() function.

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 Stackoverflow 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

这篇关于逗号分隔数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆