我有$ str字符串变量&我想从$ str string中创建一个$数组数组。
$str = "BKL|bkl,EXH|exh,FFV|ffv,LEC|lec,AUE|aue,SEM|sem";
Final array should be
$array = array(
'BKL'=> 'bkl',
'EXH' => 'exh',
'FFV' => 'ffv',
'AUE' => 'aue'
);
答案 0 :(得分:6)
这应该可以解决问题
$str = "BKL|bkl,EXH|exh,FFV|ffv,LEC|lec,AUE|aue,SEM|sem";
$final = array();
foreach (explode(',', $str) as $pair) {
list($key, $value) = explode('|', $pair);
$final[$key] = $value;
}
print_r($final);
输出
Array
(
[BKL] => bkl
[EXH] => exh
[FFV] => ffv
[LEC] => lec
[AUE] => aue
[SEM] => sem
)
答案 1 :(得分:1)
试试这个,
<?php
$str = "BKL|bkl,EXH|exh,FFV|ffv,LEC|lec,AUE|aue,SEM|sem";
$split = explode(',', $str);
$arr = array();
foreach($split as $v){
$tmp = explode('|', $v);
$arr[$tmp[0]] = $tmp[1];
}
print_r($arr);
?>
<强>输出:强>
Array
(
[BKL] => bkl
[EXH] => exh
[FFV] => ffv
[LEC] => lec
[AUE] => aue
[SEM] => sem
)
答案 2 :(得分:1)
$str = "BKL|bkl,EXH|exh,FFV|ffv,LEC|lec,AUE|aue,SEM|sem";
$result = array();
$node = explode(',', $str);
foreach ($node as $item) {
$temp = explode('|', $item);
$result[$temp[0]] = $temp[1];
}
答案 3 :(得分:0)
$str = "BKL|bkl,EXH|exh,FFV|ffv,LEC|lec,AUE|aue,SEM|sem";
$out = array;
$arr = explode(',', $str);
foreach ($arr as $item) {
$temp = explode('|', $item);
$out[$temp[0]] = $temp[1];
}
答案 4 :(得分:0)
您应该在php手册中查看explode。