$ str文字列変数があり、$str文字列から$array配列を作成したいと思います。
$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'
);
これでうまくいくはずです
$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
)
$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];
}
これを試して、
<?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
)
$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];
}
phpマニュアルの爆発を見てください。