将管道分隔符字符串分解为具有名称和值的数组
explode pipe delimiter string into array with name and value
我正在尝试分隔一个字符串,该字符串具有多个由 ; 分隔的值每个值和键由 | 分隔。我理想地希望它最终看起来像这样:
$array = array(
array( "qty" => "2", "code" => "YQTWEY33" ),
array( "qty" => "1", "code" => "YQTWEY53" )
)
我将字符串保存在一个名为 $str 的变量中,我正在使用 explode 函数分离出不同的值。但是,我不确定从这里去哪里。有人可以帮忙吗?
$str = "qty: 2 | code: YQTWEY33; qty: 1 | code: YQTWEY53";
$topics = array();
foreach (explode(';', $str) as $topic) {
list($name, $items) = explode('|', $topic);
$topics[$name] = explode(':', $items);
print_r($topics);
}
$str = "qty: 2 | code: YQTWEY33; qty: 1 | code: YQTWEY53";
$topics = array();
foreach (explode(';', $str) as $index=> $topic) {
foreach(explode('|', $topic ) as $item ) {
list($key, $value) = explode(':', $item);
$topics[$index][trim($key)] = trim($value);
}
}
print_r($topics);
我正在尝试分隔一个字符串,该字符串具有多个由 ; 分隔的值每个值和键由 | 分隔。我理想地希望它最终看起来像这样:
$array = array(
array( "qty" => "2", "code" => "YQTWEY33" ),
array( "qty" => "1", "code" => "YQTWEY53" )
)
我将字符串保存在一个名为 $str 的变量中,我正在使用 explode 函数分离出不同的值。但是,我不确定从这里去哪里。有人可以帮忙吗?
$str = "qty: 2 | code: YQTWEY33; qty: 1 | code: YQTWEY53";
$topics = array();
foreach (explode(';', $str) as $topic) {
list($name, $items) = explode('|', $topic);
$topics[$name] = explode(':', $items);
print_r($topics);
}
$str = "qty: 2 | code: YQTWEY33; qty: 1 | code: YQTWEY53";
$topics = array();
foreach (explode(';', $str) as $index=> $topic) {
foreach(explode('|', $topic ) as $item ) {
list($key, $value) = explode(':', $item);
$topics[$index][trim($key)] = trim($value);
}
}
print_r($topics);