1

$ 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'  
    );
4

5 に答える 5

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
)
于 2013-01-08T06:30:12.163 に答える
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];
}
于 2013-01-08T06:49:11.940 に答える
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
)
于 2013-01-08T06:33:42.047 に答える
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];
}
于 2013-01-08T06:30:20.040 に答える
0

phpマニュアルの爆発を見てください。

于 2013-01-08T06:32:08.323 に答える