Array reorganization code

Source: Internet
Author: User
Array reorganization code array reorganization

I now have an array as follows:
[0] => Array
(
[Size] = & gt; 123
[Path] => Array
(
[0] =>
[1] => B
[2] => c
[3] => abc
)

)

[1] => Array
(
[Size] = & gt; 456
[Path] => Array
(
[0] => d
[1] => e
[2] => f
[3] => def
)
)

How can I write code to recompose the following array styles?

[0] => Array
(
[A] => Array (
[B] => Array (
[C] => Array (
[Size] => '123 ',
[Name] => 'ABC'
)
)
)
)

[1] => Array
(
[D] => Array (
[E] => Array (
[F] => Array (
[Size] => '123 ',
[Name] => 'Def'
)
)
)
)


Reply to discussion (solution)

$arr=array(    '0' => Array    (        'size' => 123,        'path' => Array            (                '0' => 'a',                '1' => 'b',                '2' => 'c',                '3' => 'abc',            )    ),    '1' => Array    (        'size' => 456,        'path' => Array            (                '0' => 'd',                '1' => 'e',                '2' => 'f',                '3' => 'def',            )    ));$ar=array();foreach($arr as $k=>$v){     $ar[$k][$v['path'][0]][$v['path'][1]][$v['path'][2]]['size']=$v['size']; $ar[$k][$v['path'][0]][$v['path'][1]][$v['path'][2]]['name']=$v['path'][3];}print_r($ar);

$arr=array(    '0' => Array    (        'size' => 123,        'path' => Array            (                '0' => 'a',                '1' => 'b',                '2' => 'c',                '3' => 'abc',            )    ),    '1' => Array    (        'size' => 456,        'path' => Array            (                '0' => 'd',                '1' => 'e',                '2' => 'f',                '3' => 'def',            )    ));$ar=array();foreach($arr as $k=>$v){     $ar[$k][$v['path'][0]][$v['path'][1]][$v['path'][2]]['size']=$v['size']; $ar[$k][$v['path'][0]][$v['path'][1]][$v['path'][2]]['name']=$v['path'][3];}print_r($ar);


I forgot to mention that the number of elements in the path of each element in the $ arr array is different. it may be like there are only four elements above, or there may be only one or two, there may also be N.

$ Ar = array ('0' => Array ('size' => 123, 'path' => Array ('0' => 'A ', '1' => 'B', '2' => 'C', '3' => 'ABC ',)), '1' => Array ('size' => 456, 'path' => Array ('0' => 'D', '1' => 'e ', '2' => 'F', '3' => 'Def ',); foreach ($ ar as $ item) {$ t = array (); $ p = & $ t; foreach ($ item ['path'] as $ I => $ k) {if (strlen ($ k)> 1) {// Note $ item ['path'] = $ k; $ p = $ item; break;} $ p [$ k] = array (); $ p = & $ p [$ k]; unset ($ item ['path'] [$ I]);} $ res [] = $ t ;} print_r ($ res);/* Note: this ending condition is too far-fetched, but you have not given the ending condition. you can only observe the result, but it is just a reference exercise */
Array(    [0] => Array        (            [a] => Array                (                    [b] => Array                        (                            [c] => Array                                (                                    [size] => 123                                    [path] => abc                                )                        )                )        )    [1] => Array        (            [d] => Array                (                    [e] => Array                        (                            [f] => Array                                (                                    [size] => 456                                    [path] => def                                )                        )                )        ))

Thank you! you are not very familiar with PHP.

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.