展开分隔符数组
Expload Separator Array
我有一个来自数据库的代码,例如
$exp = ukuran:33,34,35;warna:putih,hitam;
我想制作一个像
这样的数组
$ukuran = array("33", "34", "35");
$warna = array("putih","hitam");
我尝试过使用 explode 但我遇到了问题。
explode(";",$exp);
结果像
Array
(
[0] => ukuran:33,34,35
[1] => warna:putih,hitam
[2] =>
)
谁能帮帮我,请问这个case怎么爆?
我个人会说像你一样继续,然后在你的结果中使用你的数组,遍历数组中的每个项目并像这样存储它
$string = 'ukuran:33,34,35;warna:putih,hitam;';
$exploded = explode(';',$string);
$master = [];
foreach($exploded as $key => $foo){
$namedKey = explode(':',$foo);
$bar = substr($foo, strpos($foo, ":") + 1); //Get everything after the ; character
$master[$namedKey[0]] = explode(",",$bar);
}
这应该 return 类似于
的结果
array(3) {
["ukuran"]=>
array(3) {
[0]=>
string(2) "33"
[1]=>
string(2) "34"
[2]=>
string(2) "35"
}
["warna"]=>
array(2) {
[0]=>
string(5) "putih"
[1]=>
string(5) "hitam"
}
}
$string = 'ukuran:33,34,35;warna:putih,hitam;';
$string = str_replace(['ukuran:', 'warna:'], '', $string);
$exploded = explode(';', $string);
$ukuran = explode(',', $exploded[0]);
$warna = explode(',', $exploded[1]);
如果你想动态地做,你不能创建变量,但你可以创建一个以类型作为键的数组:
$string = 'ukuran:33,34,35;warna:putih,hitam;';
$exploded = explode(';', $string);
$keysAndValues = [];
foreach($exploded as $exp) {
if (strlen($exp) > 0) {
$key = substr($exp, 0, strpos($exp, ':' ) );
$values = substr($exp, strpos($exp, ':' ) + 1, strlen($exp) );
$values = explode(',', $values);
$keysAndValues[$key] = $values;
}
}
这将输出:
array (size=2)
'ukuran' =>
array (size=3)
0 => string '33' (length=2)
1 => string '34' (length=2)
2 => string '35' (length=2)
'warna' =>
array (size=2)
0 => string 'putih' (length=5)
1 => string 'hitam' (length=5)
这样称呼他们:
var_dump($keysAndValues['ukuran']);
var_dump($keysAndValues['warna']);
您可以使用正则表达式分别匹配单词和数字。
然后使用 array_filter 删除空值。
$exp = "ukuran:33,34,35;warna:putih,hitam";
Preg_match_all("/(\d+)|([a-zA-Z]+)/", $exp, $matches);
Unset($matches[0]);
$matches[1] = array_filter($matches[1]);
$matches[2] = array_filter($matches[2]);
Var_dump($matches);
其实有很多方法可以做你想做的事,但如果我是你,我会试试这个方法:)
<?php
$exp = 'ukuran:33,34,35;warna:putih,hitam';
$result = explode(';',$exp);
foreach($result as $k=>$v){
$key_value = explode(':',$v);
// this line will help your to treat your $ukuran and $warna as array variable
${$key_value[0]} = explode(',',$key_value[1]);
}
print '<pre>';
print_r($ukuran);
print_r($warna);
print '</pre>';
?>
我有一个来自数据库的代码,例如
$exp = ukuran:33,34,35;warna:putih,hitam;
我想制作一个像
这样的数组$ukuran = array("33", "34", "35");
$warna = array("putih","hitam");
我尝试过使用 explode 但我遇到了问题。
explode(";",$exp);
结果像
Array
(
[0] => ukuran:33,34,35
[1] => warna:putih,hitam
[2] =>
)
谁能帮帮我,请问这个case怎么爆?
我个人会说像你一样继续,然后在你的结果中使用你的数组,遍历数组中的每个项目并像这样存储它
$string = 'ukuran:33,34,35;warna:putih,hitam;';
$exploded = explode(';',$string);
$master = [];
foreach($exploded as $key => $foo){
$namedKey = explode(':',$foo);
$bar = substr($foo, strpos($foo, ":") + 1); //Get everything after the ; character
$master[$namedKey[0]] = explode(",",$bar);
}
这应该 return 类似于
的结果array(3) {
["ukuran"]=>
array(3) {
[0]=>
string(2) "33"
[1]=>
string(2) "34"
[2]=>
string(2) "35"
}
["warna"]=>
array(2) {
[0]=>
string(5) "putih"
[1]=>
string(5) "hitam"
}
}
$string = 'ukuran:33,34,35;warna:putih,hitam;';
$string = str_replace(['ukuran:', 'warna:'], '', $string);
$exploded = explode(';', $string);
$ukuran = explode(',', $exploded[0]);
$warna = explode(',', $exploded[1]);
如果你想动态地做,你不能创建变量,但你可以创建一个以类型作为键的数组:
$string = 'ukuran:33,34,35;warna:putih,hitam;';
$exploded = explode(';', $string);
$keysAndValues = [];
foreach($exploded as $exp) {
if (strlen($exp) > 0) {
$key = substr($exp, 0, strpos($exp, ':' ) );
$values = substr($exp, strpos($exp, ':' ) + 1, strlen($exp) );
$values = explode(',', $values);
$keysAndValues[$key] = $values;
}
}
这将输出:
array (size=2)
'ukuran' =>
array (size=3)
0 => string '33' (length=2)
1 => string '34' (length=2)
2 => string '35' (length=2)
'warna' =>
array (size=2)
0 => string 'putih' (length=5)
1 => string 'hitam' (length=5)
这样称呼他们:
var_dump($keysAndValues['ukuran']);
var_dump($keysAndValues['warna']);
您可以使用正则表达式分别匹配单词和数字。
然后使用 array_filter 删除空值。
$exp = "ukuran:33,34,35;warna:putih,hitam";
Preg_match_all("/(\d+)|([a-zA-Z]+)/", $exp, $matches);
Unset($matches[0]);
$matches[1] = array_filter($matches[1]);
$matches[2] = array_filter($matches[2]);
Var_dump($matches);
其实有很多方法可以做你想做的事,但如果我是你,我会试试这个方法:)
<?php
$exp = 'ukuran:33,34,35;warna:putih,hitam';
$result = explode(';',$exp);
foreach($result as $k=>$v){
$key_value = explode(':',$v);
// this line will help your to treat your $ukuran and $warna as array variable
${$key_value[0]} = explode(',',$key_value[1]);
}
print '<pre>';
print_r($ukuran);
print_r($warna);
print '</pre>';
?>