本文实例讲述了php一个解析字符串排列数组的方法。分享给大家供大家参考。具体如下:
<?php$str="1|苹果|301|桃子|501|普通|102|小麦|1002|玉米|352|大米|303|电脑|53|MP3|1213|打印机|8";$strArray=explode("\n",$str);$result=array();foreach($strArray as $item){ $itemArray=explode('|',$item); $result[$itemArray[0]][]=$item;}echo "<pre>";print_r($result);echo "<pre>";?>运行结果如下:
希望本文所述对大家的php程序设计有所帮助。