The example in this article tells a PHP method for parsing an array of string permutations. Share to everyone for your reference. Specifically as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21-22 |
<?php $str = "1| apple |30 1| Peach |50 1| general |10 2| wheat |100 2| Corn |35 2| Rice |30 3| computer |5 3| mp3|121 3| printer |8 "; $strArray =explode ("n", $str); /* The key is the following code * * * $result =array (); foreach ($strArray as $item) {$itemArray =explode (' | ', $item); $result [$itemArray [0]][]= $item;} echo "<pre>"; Print_r ($result); echo "<pre>";?> |
The results of the operation are as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21-22 |
<pre>array ([1] => Array ([0] => 1| Apple |30 [1] => 1| peach |50 [2] => 1| general |10) [2] => Array ([0] => 2 | wheat |100 [1] => 2| corn |35 [2] => 2| rice |30) [3] => Array ([0] => 3| computer |5 [1] => 3| mp3|121 [2] => 3| printer |8)) <pre> |
I hope this article will help you with your PHP program design.