How to convert a multidimensional array to one dimension

Source: Internet
Author: User
How to convert a multi-dimensional array into a one-dimensional multi-dimensional array to print the results:
Array ([10] => prepare to buy [11] => do not prepare to change [12] => Array ([0] => 3-car [1] => City SUB) [13] => 33333 [14] => Array ([0] => medium [1] => small-sized) [15] => Full risk)
If you want to convert it to a one-dimensional one, keep the original KEY unchanged, and merge multiple dimensions, you finally want
Array ([10] => prepare to buy [11] => not prepare to change [12] => 3-car, city SUB [13] => 33333 [14] => medium and small-sized [15] => Full Insurance)

After a long time, I am confused. Please take a look


Reply to discussion (solution)

$ A = Array (10 => 'buy now ', 11 =>' do not change ', 12 => Array (0 => 'sedan 3 ', 1 => 'City sub'), 13 => 33333, 14 => Array (0 => 'medium-sized ', 1 => 'clever type '), 15 => 'quanshen',); foreach ($ a as $ k => $ v) {if (is_array ($ v )) $ a [$ k] = join (',', $ v);} print_r ($ );
Array ([10] => prepare to buy [11] => not prepare to change [12] => 3-car, city SUB [13] => 33333 [14] => medium and small-sized [15] => Full Insurance)

Upstairs is a positive solution.

$ A = array (10 => 'buy now ', 11 =>' do not change ', 12 => Array (0 => 'sedan 3 ', 1 => 'City sub'), 13 => 33333, 14 => Array (0 => 'medium-sized ', 1 => 'clever type '), 15 => 'completely dangerous ',); $ B = array (); foreach ($ a as $ k => $ v) {$ B [$ k] = $ v; if (is_array ($ v) {$ B [$ k] = $ v [0]. ','. $ v [1] ;}} print_r ($ B );


Array ([10] => prepare to buy [11] => not prepare to change [12] => 3-car, city SUB [13] => 33333 [14] => medium and small-sized [15] => Full Insurance)

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.