PHP array processing for help. thanks for your help. This post is finally edited by a858070363 from 2015-05-0216:29:12. 1. the data submitted in the form is: value 1, value 2, value 3, value 4, value 5, value 6 ...... 2. I use The explode function to process the content submitted by the form and convert it into an array. my current problem is: remove one value for every three, convert the final array to the PHP array format at the beginning for help. thank you for your help.
This post was last edited by a858070363 at 16:29:12 on
1. the data submitted by the form is: value 1, value 2, value 3, value 4, value 5, value 6 ......
2. I use The explode function to process the content submitted by the form and convert it into an array.
My current problem is:
Remove a value from each three, and convert the last array to the initial format, for example, value 1, value 2, value 4, value 5.
How to implement it using code?
I have been tossing for two days. I am not sure. please help me. Thank you.
------ Solution ----------------------
$ Data = 'value 1, value 2, value 3, value 4, value 5, value 6 ';
$ DataArr = explode (',', $ data );
Foreach ($ dataArr as $ key => $ value ){
If ($ key % 3 = 2 ){
Unset ($ dataArr [$ key]);
}
}
$ Res = implode (',', $ dataArr );
Echo $ res;
Just split the traversal and merge it.
------ Solution ----------------------
This post was last edited by xuzuning at 17:46:32 on
$ N = 5; // Each 5
$ S = 'value 1, value 2, value 3, value 4, value 5, value 6, value 7, value 8, value 9, value 10, value 11 ';
$ A = explode (',', $ s );
Foreach ($ a as $ k => $ v) if ($ k % $ n = $ n-1) unset ($ a [$ k]);
$ Res = join (',', $ );
Echo $ res;
Value 1, value 2, value 3, value 4, value 6, value 7, value 8, value 9, value 11
However, the meaning of a few is different from that of each other. Adjust it by yourself
------ Solution ----------------------
$ Key % 3 = 2 Can you understand this?
When I write $ n for 3, can I write $ n-1 for 2?