如果我有一个字符串:
123*23*6594*2*-10*12
如何在由*
分隔的字符串中提取单个数字?也就是说,我想要这个输出:
a=123, b=23, c=6594, d=2, e=-10, f=12.
答案 0 :(得分:3)
弹性
$vars = range('a', 'z');
$vals = explode('*', $string);
$result = array_combine(array_slice($vars, 0, count($vals)), $vals);
结果:
Array
(
[a] => 123
[b] => 23
[c] => 6594
[d] => 2
[e] => -10
[f] => 12
)
答案 1 :(得分:0)
类似的东西:
list($a,$b,$c,$d,$e,$f) = explode("*", $str);
只是一个猜测;)
答案 2 :(得分:0)
您可以使用简单:
$str = "123*23*6594*2*-10*12";
$arr = explode("*",$str);
$arr['a']=$arr[0];
$arr['b']=$arr[1];
$arr['c']=$arr[2];
$arr['d']=$arr[3];
$arr['e']=$arr[4];
$arr['f']=$arr[5];
echo "a=" .$arr['a'] ." b=". $arr['b']." c=". $arr['c']." d=". $arr['d']." e=". $arr['e']." f=". $arr['f'];
修改强>
完成Rizier123的愿望:
$str = "123*23*6594*2*-10*12";
$arr = explode("*",$str);
$vars = range('a', 'z');
foreach ($arr as $val => $key){
echo $vars[$val]."=".$key."<br>";
}
答案 3 :(得分:0)
仅仅是为了将结果设置为可迭代(而不是实际数组)与alpha键略有不同的方式:
$data = '123*23*6594*2*-10*12';
function dataseries($data) {
$key = 'a';
while (($x = strpos($data, '*')) !== false) {
yield $key++ => substr($data, 0, $x);
$data = substr($data, ++$x);
}
yield $key++ => $data;
}
foreach(dataseries($data) as $key => $value) {
echo $key, ' = ', $value, PHP_EOL;
}
需要PHP&gt; = 5.5.0