2
$str = 'answers[0] = "Love it"; answers[1] = "Hate it."; answers[2] = "Doesnt care"; answers[3] = "Like it";'

どうすれば配列に変換できますか?

4

6 に答える 6

1
$str = 'answers[0] = "Love it"; answers[1] = "Hate it."; answers[2] = "Doesnt care"; answers[3] = "Like it";';

preg_match_all('/"(.*)";/U', $str, $matches);
$arr = $matches[1];

var_dump($arr);
于 2013-09-26T10:30:50.403 に答える
1

;セミコロン ( )の後に新しい行を追加できる場合は、parse_ini_string()関数を見てください。

$str   = str_replace(';', "\n", $str); // <- add them in the source if you can
$array = parse_ini_string($str);
于 2013-09-26T10:43:14.583 に答える
1
$str = 'answers[0] = "Love it"; answers[1] = "Hate it."; answers[2] = "Doesnt care"; answers[3] = "Like it";';
$chunks = explode(';', $str);

for ($aa=0;$aa<count($chunks);$aa++)
{
    $chunks1 = explode('=', $chunks[$aa]);
    $myArray[]=$chunks1[1];
}
于 2013-09-26T10:26:53.753 に答える
0

このようなもの?

<?php
$str = '$answers[0] = "Love it"; $answers[1] = "Hate it."; $answers[2] = "Doesnt care"; $answers[3] = "Like it";';
eval($str);
print_r($answers);

?>
于 2013-09-26T10:18:36.927 に答える