Home > Backend Development > PHP Tutorial > 对数组进展分组

对数组进展分组

WBOY
Release: 2016-06-13 11:58:07
Original
989 people have browsed it

对数组进行分组
我现在想实现这么个功能,
Array ( [tag] => id[type] => complete [level] => 4 [value] => 01 )
Array ( [tag] => name[type] => complete [level] => 4 [value] => 小张 )
Array ( [tag] => flag[type] => complete [level] => 4 )
Array ( [tag] => id [type] => complete [level] => 4 [value] => 02 )
Array ( [tag] => name [type] => complete [level] => 4 [value] => 小李 )
Array ( [tag] => flag [type] => complete [level] => 4 )
Array ( [tag] => id [type] => complete [level] => 4 [value] => 03 )
Array ( [tag] => name [type] => complete [level] => 4 [value] => 小刘 )
Array ( [tag] => flag [type] => complete [level] => 4 ) 

这几个数组是我通过解析xml文件得到打印出来的,他们每三个数组是一组,我需要把id, name, flag写入数据库,以上这种情况就是写三条数据进数据库。但是我取得的xml文件不固定,内容也不固定,所以每次不一定就是三个一组,我想做个通用的方法,把数组分类,然后循环存入数据库有什么好办法吗?各位大神?
------解决方案--------------------

本帖最后由 xuzuning 于 2014-05-26 17:21:24 编辑
$a = array(<br />  Array ( 'tag' => 'id',   'type' => 'complete', 'level' => 4, 'value' => '01' ),<br />  Array ( 'tag' => 'name', 'type' => 'complete', 'level' => 4, 'value' => '小张' ),<br />  Array ( 'tag' => 'flag', 'type' => 'complete', 'level' => 4, ),<br />  Array ( 'tag' => 'id',   'type' => 'complete', 'level' => 4, 'value' => '02' ),<br />  Array ( 'tag' => 'name', 'type' => 'complete', 'level' => 4, 'value' => '小李' ),<br />  Array ( 'tag' => 'flag', 'type' => 'complete', 'level' => 4, ),<br />  Array ( 'tag' => 'id',   'type' => 'complete', 'level' => 4, 'value' => '03' ),<br />  Array ( 'tag' => 'name', 'type' => 'complete', 'level' => 4, 'value' => '小刘' ),<br />  Array ( 'tag' => 'flag', 'type' => 'complete', 'level' => 4, ),<br />);<br /><br />$st = array();<br />foreach($a as $t) {<br />  if(isset($st[$t['tag']])) {<br />    //完成你需要的操作<br />    $st = array();<br />  }<br />  $st[$t['tag']] = $t;<br />}<br />
Copy after login
应该看得懂吧
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template