分解字符串并为带有分隔符前面文本的数组设置键?

2023-12-28

有没有办法接受这样的输入:

|
testing==one two three
|
setting==more testing
|

并得到这样的东西

array['testing'] = "one two three";
array['setting'] = "more testing"

现在,我只是分解字符串并使用编号索引设置数组,但我希望用户能够以任何顺序输入项目,并且能够使用带有第一个值中的键的数组。

function get_desc_second_part(&$value)  {
  list(,$val_b) = explode('==',$value);
  $value = trim($val_b);
}

Thanks!


像这样的东西吗?管道增加了一些可能不必要的复杂性(分隔符可以是新行):

$arr = array();
foreach (explode('|', $str_input) as $line) {
    $l = explode('==', trim($line));
    if (isset($l[1]))
        $arr[$l[0]] = $l[1];
}
print_r($arr);

/*
Array
(
    [testing] => one two three
    [setting] => more testing
)
*/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

分解字符串并为带有分隔符前面文本的数组设置键? 的相关文章

随机推荐