尝试使用PHP操作数组

尝试使用PHP操作数组,php,mysql,Php,Mysql,我不想问,但我对PHP还是很陌生,我被卡住了 我在MySQL中存储了一个逗号分隔的列表;蓝色、12、红色、15等。 我可以把它拿出来,做我想做的大部分事情,但我对如何继续感到困惑。 最后,我想更改数据的输出 蓝色、12、红色、15、 到 Blue=>12,Red=>15(不带最后一个逗号),这样我就可以在我试图构建的程序中使用数据 目前,我能够实现: 蓝色、=>12、=>红色、=>15、 代码: $result=$con->query($sql); 如果($result->num_rows>0)

我不想问,但我对PHP还是很陌生,我被卡住了

我在MySQL中存储了一个逗号分隔的列表;蓝色、12、红色、15等。 我可以把它拿出来,做我想做的大部分事情,但我对如何继续感到困惑。 最后,我想更改数据的输出

蓝色、12、红色、15、

Blue=>12,Red=>15(不带最后一个逗号),这样我就可以在我试图构建的程序中使用数据

目前,我能够实现:
蓝色、=>12、=>红色、=>15、

代码:

$result=$con->query($sql);
如果($result->num_rows>0){
而($row=$result->fetch_assoc()){
$id=$row['id'];
$type=$row[“数据名”];
$datas=$type;
eval(“\$test=array(“.$datas”);”;
foreach($test作为$test)
{
echo“$test,=>”;
}
}
}
使用所需的输出,我将能够从表单中输入数据以创建SVGGraph


提前感谢您的帮助。

使用eval是最糟糕的

以下是我将如何做到这一点,同时尽可能遵守您的代码:

$result = $con->query($sql);

if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {
        $id                 = $row['id'];
        $type               = $row["dataname"];
        $datas = $type;

        $res = array();
        $arr = explode(",", str_replace(" ","",$datas));
        for ($i = 0; $i < count($arr); $i+=2) {            
            $res[$arr[$i]] = $arr[$i + 1];
        }

        foreach($res as $key=>$value)
        {
            echo "<option name='$value'>$key , =></option>";
        }
    }
}
$result=$con->query($sql);
如果($result->num_rows>0){
而($row=$result->fetch_assoc()){
$id=$row['id'];
$type=$row[“数据名”];
$datas=$type;
$res=array();
$arr=explode(“,”,str_replace(“,”,$datas));
对于($i=0;$i$value)
{
回显“$key,=>”;
}
}
}

首先,尝试不要使用eval-。)

第二,试着把你需要的所有数据放到一个大字符串中。然后可以使用PHP函数将字符串转换为单个元素。最后一件事是迭代数组,将第一项作为键,第二项作为元素分配到另一个数组中


我将把实际实现留给您作为PHP编码技能的练习。:)

好的,首先,我将尝试在将来将这些信息存储在单独的行中,逗号分隔的列表用于我们没有数据库的情况下(例如,简单的文本文件)

但要回答您的问题(假设结果是分隔值的字符串):

$result=分解(',',$result);
$output=[];
对于($i=0;$i$result[i+1]);
}
//输出:
$str=“”
foreach($key=>$value的输出){
str.=$key.'=>'.$value.',';
}
$str=rtrim($str,,');
echo$str//这将是您的输出

请不要使用eval。你几乎总是可以避免它,这是危险的。 以下是一个不需要评估的解决方案:

$result = $con->query($sql);

if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {
        $id = $row['id'];
        $type = $row["dataname"];  // Thats our comma-seperated list, right?
        $arr = explode(',', $type); // Make $type to array
        array_pop($arr); // Delete last element because its empty

        // We go through the array with step = 2
        // because the first is always the key and the second the value
        for($i = 0; $i < count($arr); $i += 2) 
        {
             echo "<option name='$arr[$i+1]'>$arr[$i] , =></option>";
        }
    }
}
$result=$con->query($sql);
如果($result->num_rows>0){
而($row=$result->fetch_assoc()){
$id=$row['id'];
$type=$row[“dataname”];//这是我们用逗号分隔的列表,对吗?
$arr=explode(“,”,$type);//将$type设置为数组
array_pop($arr);//删除最后一个元素,因为它是空的
//我们用step=2遍历数组
//因为第一个始终是键,第二个始终是值
对于($i=0;$i”;
}
}
}
为什么eval是邪恶的: 字符串中的任何内容都将作为代码从脚本中转义,脚本拥有所有权限,包括文件访问等。由于eval的字符串来自数据库,所以您甚至不能绝对确定在使用eval时执行的代码是否正确。 此外,eval在调试方面是不好的。 最后:为什么要用可以避免的东西来制造开销? 由于所有这些原因,在使用eval时,它通常被认为是不好的风格。最好永远不要习惯它

//从右边修剪逗号
//trim comma from the right
$str = rtrim('Blue, 12, Red, 15,', ',');

//create a helper array
$array = explode(', ', $str);

//arrange elements in the new array
for ($i = 0; $i < count($array) - 1; $i = $i + 2) {
    $new[] = $array[$i] . ' => ' . $array[$i + 1];
}

//output new elements
echo implode(', ', $new);
$str=rtrim('蓝色,12,红色,15,',','); //创建助手数组 $array=explode(“,”,$str); //在新数组中排列元素 对于($i=0;$i。$array[$i+1]; } //输出新元素 回波内爆(',',$new);
如果我理解正确,您可以从字符串到数组再到字符串。 如果是这样,可以使用正则表达式跳过数组。根据字符串的长度,创建一个巨大的数组可能是一个问题

所以我想出了这些例子:

$input = "Blue, 1, Red, 2, Green, 3, Violet, 4,";

// Output: Blue => 1, Red => 2, Green => 3, Violet => 4
echo rtrim(preg_replace('/(([^,]*),([^,]*)),+/', '\2 =>\3,', $input), ',');

// Output <option name="Blue">Blue => 1</option><option name="Red">Red => 2</option><option name="Green">Green => 3</option><option name="Violet">Violet => 4</option>
echo preg_replace('/(\s*([^,]*)\s*,\s*([^,]*)\s*),+/', '<option name="\2">\2 => \3</option>', $input);
$input=“蓝色,1,红色,2,绿色,3,紫色,4,”;
//输出:蓝色=>1,红色=>2,绿色=>3,紫色=>4
echo rtrim(preg_replace('/([^,]*),([^,]*),+/','\2=>\3',,$input),',');
//输出蓝色=>1红色=>2绿色=>3紫色=>4
echo preg_替换('/(\s*([^,]*)\s*,\s*([^,]*)\s*),+/','\2=>\3',$input);
正如您所看到的,不涉及循环

我希望有帮助

编辑

下面是可视化正则表达式的链接


您可以发布
var\u dump($datas)
的输出吗?拆分逗号上的字符串,array\u walk从值中修剪空格,array\u chunk使用值2拆分名称和数字,生成一个2d数组;然后对第0列和第1列使用array_combine()和array_column()创建键/值对使用
eval
这完全是胡说八道。“我在MySQL中存储了一个逗号分隔的列表”-这是您应该开始改进的地方,通过正确规范化数据库数据结构。使用
str_replace()
删除逗号<代码>$test=str_替换(“,”,“,”$tests)
$arr.pop()?你最近可能花了太多时间在JavaScript上……:)你能解释一下为什么eval是危险的吗?谢谢,这很有效。然而,我对此做了一点修改,以便做更多我需要的事情(对不起,这在我的OP中是不正确的):
echo“$key
//trim comma from the right
$str = rtrim('Blue, 12, Red, 15,', ',');

//create a helper array
$array = explode(', ', $str);

//arrange elements in the new array
for ($i = 0; $i < count($array) - 1; $i = $i + 2) {
    $new[] = $array[$i] . ' => ' . $array[$i + 1];
}

//output new elements
echo implode(', ', $new);
$input = "Blue, 1, Red, 2, Green, 3, Violet, 4,";

// Output: Blue => 1, Red => 2, Green => 3, Violet => 4
echo rtrim(preg_replace('/(([^,]*),([^,]*)),+/', '\2 =>\3,', $input), ',');

// Output <option name="Blue">Blue => 1</option><option name="Red">Red => 2</option><option name="Green">Green => 3</option><option name="Violet">Violet => 4</option>
echo preg_replace('/(\s*([^,]*)\s*,\s*([^,]*)\s*),+/', '<option name="\2">\2 => \3</option>', $input);