假设我有一个像这样存储的数组。
Array ( 
   [0] => width: 650px;border: 1px solid #000; 
   [1] => width: 100%;background: white; 
   [2] => width: 100%;background: black; 
) 如何通过分隔";“将数组字符串拆分成多个片段?然后我想再次将它们保存在数组中,或者将它们显示出来。我怎么发动汽车呢?
Array(
   [0] => width: 650px
   [1] => border: 1px solid #000
)有什么想法吗?在高级中感谢
发布于 2010-07-15 11:02:55
我个人会使用preg_split来去掉从最后一个分号开始出现的额外数组元素……
$newarray = array();
foreach ($array as $i => $styles):
    // Split the statement by any semicolons, no empty values in the array
    $styles = preg_split("/;/", $styles, -1, PREG_SPLIT_NO_EMPTY);
    // Add the semicolon back onto each part
    foreach ($styles as $j => $style) $styles[$j] .= ";";
    // Store those styles in a new array
    $newarray[$i] = $styles;
endforeach;编辑:不要在每行中添加分号:
$newarray = array();
foreach ($array as $i => $styles):
    // Split the statement by any semicolons, no empty values in the array
    $newarray[$i] = preg_split("/;/", $styles, -1, PREG_SPLIT_NO_EMPTY);
endforeach;哪一项应该输出:
Array(
   [0] => width: 650px;
   [1] => border: 1px solid #000;
)与explode不同,explode应输出:
Array(
   [0] => width: 650px;
   [1] => border: 1px solid #000;
   [2] => ;
)发布于 2010-07-15 10:55:00
explode命令:
explode(';', $array);然后,您必须将';‘附加到每个字符串的末尾。
发布于 2010-07-15 11:02:50
一个例子
foreach($array as $item) {
   $mynewarray = explode(";",$item);
   foreach($mynewarray as $newitem) {
        $finalarray[] = $newitem.";";
   }
   //array is ready
}https://stackoverflow.com/questions/3252160
复制相似问题