For processing PHP arrays, thank you!

Source: Internet
Author: User
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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.