Array loop output

Source: Internet
Author: User
Array loop output
$ A = M ('A'); // read data from database a table (array) $ B = M ('B '); // read data from table A (array) foreach ($ a as $ k = >$ v) {echo $ v ;} foreach ($ B as $ kk =>$ vv) {echo $ vv;} // note: the code is only for better description // read 3 $ v records and then read 1 $ vv recyclically read 3 $ v records, 1 $ vv records, and keep repeating, stop the output until the $ v loop output is complete.

$ A and $ B are arrays, and the loop is to display at the front end. each of the three table a data entries is displayed, insert an advertisement (note: The advertisement is in Table B of the database) data Display cannot be repeated. How can this problem be achieved?


Reply to discussion (solution)

$ A = array (, 10, 13); $ B = array ('ad 1', 'ad 2 ', 'Ad 3'); $ newa = array_chunk ($ a, 3); foreach ($ B as $ k = >$ v) {array_push ($ newa [$ k], $ v) ;}$ data = array (); foreach ($ newa as $ v) {$ data = array_merge ($ data, $ v);} print_r ($ data ); /* Array ([0] => 1 [1] => 2 [2] => 3 [3] => advertisement 1 [4] => 4 [5] => 5 [6] => 6 [7] => advertisement 2 [8] => 7 [9] => 8 [10] => 9 [11] => advertisement 3 [12] => 10 [13] => 11 [14] => 12 [15] => 13) */

$ A = array (, 10, 13); $ B = array ('ad 1', 'ad 2 ', 'Ad 3'); $ newa = array_chunk ($ a, 3); foreach ($ B as $ k = >$ v) {array_push ($ newa [$ k], $ v) ;}$ data = array (); foreach ($ newa as $ v) {$ data = array_merge ($ data, $ v);} print_r ($ data ); /* Array ([0] => 1 [1] => 2 [2] => 3 [3] => advertisement 1 [4] => 4 [5] => 5 [6] => 6 [7] => advertisement 2 [8] => 7 [9] => 8 [10] => 9 [11] => advertisement 3 [12] => 10 [13] => 11 [14] => 12 [15] => 13) */


Good idea. It is worth learning!

$ A = array (, 10, 13); $ B = array ('ad 1', 'ad 2 ', 'Ad 3'); $ g = 0; foreach ($ a as $ k => $ v) {echo $ v.'
'; If ($ k & $ k % 3 = 0) echo @ $ B [$ g ++].'
';}
1234 advertisement 1567 advertisement 28910 advertisement 3111213

$ A = array (, 10, 13); $ B = array ('ad 1', 'ad 2 ', 'Ad 3'); $ g = 0; foreach ($ a as $ k => $ v) {echo $ v.'
'; If ($ k & $ k % 3 = 0) echo @ $ B [$ g ++].'
';}
1234 advertisement 1567 advertisement 28910 advertisement 3111213


If the key of $ B is not a number, isn't this method unfeasible? Because both $ a and $ B are obtained from the database, they are actually associated arrays.

$ A = array (, 10, 13); $ B = array ('ad 1', 'ad 2', 'ad 3 ', '',''); $ t = array_map ('Array _ merge', array_chunk ($ a, 3), array_chunk ($ B, 1 )); print_r ($ t );
Array ([0] => Array ([0] => 1 [1] => 2 [2] => 3 [3] => advertisement 1) [1] => Array ([0] => 4 [1] => 5 [2] => 6 [3] => advertisement 2) [2] => Array ([0] => 7 [1] => 8 [2] => 9 [3] => advertisement 3) [3] => Array ([0] => 10 [1] => 11 [2] => 12 [3] =>) [4] => Array ([0] => 13 [1] => ))

It is not a subscript array. you can set another counter
You can also use array_values to program the subscript array.

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.