将格式化字符串解析为数组的数组
+2-1+18*+7-21+3*-4-5+6x29
上面的字符串是我尝试拆分为 key => 的字符串类型的示例。值数组或类似的东西。该字符串用于表示 Intranet 站点的三列页面上各种类的布局,用户可以通过拖放对其进行编辑。该字符串存储在 cookie 中,以便下次访问时使用。
数字代表类的 id,-
、+
和 x
代表类的状态(最小化、展开或隐藏), *
表示分栏符。
我可以使用爆炸轻松地将其分成几列,它给出了带有 3 $key => 的数组$value
关联。
例如。
$column_layout = array( [0] => '+2-1+18' , [1] => '+7-21+3' , [2] => '-4-5+6x29' )
然后我需要将其分成不同的类,将状态和 ID 保持在一起。由于不同的类别和状态会因用户而异,以及每列有多少个类别和状态,因此我需要能够自动完成这一切。
$column1 = array(
array( '+' , 2 ),
array( '-' , 1 ),
array( '+' , 18 )
);
$column2 = array(...
+2-1+18*+7-21+3*-4-5+6x29
The above string is an example of the kind of string I'm trying to split into either a key => value array or something similar. The string is used to represent the layout of various classes on a three column page of an intranet site, which is editable by the user via drag and drop. This string is stored in a cookie to be used on the next visit.
The numbers represent the id of a class and -
, +
and x
represent the state of the class (minimised, expanded or hidden), the *
represents a column break.
I can split this into the columns easily using explode which gives and array with 3 $key => $value
associations.
eg.
$column_layout = array( [0] => '+2-1+18' , [1] => '+7-21+3' , [2] => '-4-5+6x29' )
I then need to split this into the various classes from there, keeping the status and id together. As the different classes and statuses will change from user to user and how many there are for each column, I need to be able to do this all automatically.
$column1 = array(
array( '+' , 2 ),
array( '-' , 1 ),
array( '+' , 18 )
);
$column2 = array(...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先
explode()
带有分隔符*
的数组,然后您可以使用 preg_match_all 来匹配分解数组中的每个项目。类似的东西适用于您的示例输入。
您的示例的输出最终如下所示:
使用 PHP 5.3,您可以使用类似的内容(未经测试)。主要区别在于内部循环已替换为
array_map
这消除了对大量代码行的需要。 (数组映射将函数应用于数组中的每个项目并返回转换后的数组)。漂亮的 闭包语法 需要 PHP 5.3您也可以完全删除循环:
然而,这有一个很大的缺点,即对于大多数 PHP 开发人员来说不太可读,这就是为什么我不建议使用它。
更多解释
应 @Christopher Altman 的要求
PHP 5.3 版本中唯一的新功能实际上是这样的:
扩展和稍微改变一下(作为示例)
因此,如果
$myArray
定义为当它通过数组映射函数运行时,您可以将其视为将其转换为
But as PHP 不是 惰性语言,所有函数一遇到就会被求值,所以数组从 array_map 返回为:
在实际代码中,
preg_match_all
返回一个匹配数组(其中匹配是数组)。因此,我所做的就是获取数组,并在每次匹配时应用一个函数,将匹配项转换为所需格式的不同数组。First
explode()
the array with the delimiter*
You could then use preg_match_all to match each item in the exploded array. Something like this works with your example input.
The output from your example ends up like this:
With PHP 5.3 you could use something like this (untested). The main difference is that the inner loop has been replaced by
array_map
which removes the need for a lot of lines of code. (Array map applies a function to every item in an array and returns the transformed array). PHP 5.3 is required for the nice closure syntaxYou could also remove the loops altogether:
However this has the large disadvantage of not being very readable to most PHP developers which is why I wouldn't recommend using it.
More explanation
At the request of @Christopher Altman
The only new bit in the PHP 5.3 version is really this:
Expanding and altering it a bit (as an example)
So if
$myArray
is defined asWhen it is run through the array map function you can think of it as converting it into
But as PHP isn't a lazy language, all the functions are evaluated as soon as they are encountered, so the array is returned from array_map as:
In the actual code,
preg_match_all
returns an array of matches (where the matches are arrays). So all I do is take the array and on every match apply a function that converts the match into a different array in the required format.假设严格格式化的输入具有静态数量的段和每个段的值,则使用
sscanf()
作为解析字符串的(详细)直接方法(而不是)有一些优点preg_
技术。preg_match()
那样生成无用的“全字符串匹配”。$matches
数组中挑选出您需要的内容(如使用preg_match()
)代码: (Demo)
输出:
ps
Assuming that your strictly formatted input has a static number of segments and values per segment, there are some advantages to using
sscanf()
as a (verbose) direct way to parse the string instead of apreg_
technique.preg_match()
does.$matches
array (like withpreg_match()
)Code: (Demo)
Output:
p.s.
sscanf()
.