How to implement vertical and horizontal conversion of arrays

Source: Internet
Author: User
How to implement array vertical to horizontal? array (6 & gt; array (5 & gt; xx, & nbsp; 42 & gt; XXL), 7 & gt; array (5 & gt; xx, & nbsp; 40 & gt; XL), 8 & gt; array (8 & gt; Gray, & nbsp; 40 & gt; XL),); result: how to implement array (0 & array vertical to horizontal

Array (
6 => array (5 => 'Black xx', 42 => 'XXL '),
7 => array (5 => 'Black xx', 40 => 'XL '),
8 => array (8 => 'grayed ', 40 => 'XL '),
);

// Result:
Array (
0 => array (5 => 'Black xx', 8 => 'grayed '),
1 => array (42 => 'XXL', 40 => 'XL '),
);


How does one implement vertical conversion of arrays and filter repeated values?



------ Solution --------------------

$ Arr = array (
6 => array (5 => 'Black xx', 42 => 'XXL '),
7 => array (5 => 'Black xx', 40 => 'XL '),
8 => array (8 => 'grayed ', 40 => 'XL '),
);

$ Result = array ();

Foreach ($ arr as $ ){
$ Keys = array_keys ($ );
$ Vals = array_values ($ );
$ Result [0] [$ keys [0] = $ vals [0];
$ Result [1] [$ keys [1] = $ vals [1];
}

Print_r ($ result );



Array
(
[0] => Array
(
[5] => Black xx
[8] => Gray
)

[1] => Array
(
[42] => XXL
[40] => XL
)

)

------ Solution --------------------
Simplest writing method
$ A = array (
6 => array (5 => 'Black xx', 42 => 'XXL '),
7 => array (5 => 'Black xx', 40 => 'XL '),
8 => array (8 => 'grayed ', 40 => 'XL '),
);

Foreach ($ a as $ r ){
$ I = 0;
While (list ($ k, $ v) = each ($ r) $ B [$ I ++] [$ k] = $ v;
}
Print_r ($ B );
Array
(
[0] => Array
(
[5] => Black xx
[8] => Gray
)

[1] => Array
(
[42] => XXL
[40] => XL
)

)

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.