我有这个:
$var = 'es@set:col-md-8 col-lg-4 col-xl-6|info:f1|but:md
fs@set:col-md-4 col-lg-8 col-xl-6|info:f4|but:md';
我想在这里得到它:
$arr = array(
'es' => array(
'set' => 'col-md-8 col-lg-4 col-xl-6',
'info' => 'f1',
'but' => 'md'),
'fs' => array(
'set' => 'col-md-4 col-lg-8 col-xl-6',
'info' => 'f4',
'but' => 'md'),
);
许多爆炸:“ \ n”“ @”“ |” “:”
任何帮助将不胜感激。
谢谢!
答案 0 :(得分:1)
这需要循环遍历在换行符上展开的字符串,在@
上展开以获取键/值对,在|
上展开值以获取键的各个项,最后通过分解:
$var = 'es@set:col-md-8 col-lg-4 col-xl-6|info:f1|but:md
fs@set:col-md-4 col-lg-8 col-xl-6|info:f4|but:md';
$arr = array();
foreach (explode("\n", $var) as $item) {
list($key, $value) = explode('@', $item);
foreach (explode('|', $value) as $val) {
list ($k, $v) = explode(':', $val);
$arr[$key][$k] = $v;
};
}
print_r($arr);
输出:
Array (
[es] => Array (
[set] => col-md-8 col-lg-4 col-xl-6
[info] => f1
[but] => md
)
[fs] => Array (
[set] => col-md-4 col-lg-8 col-xl-6
[info] => f4
[but] => md
)
)