How to generate multi-dimensional arrays from one-dimensional arrays

Source: Internet
Author: User
I have never quite understood how to quickly generate a multi-dimensional array from a one-dimensional array: I have a field that is a string and the value may be 1 or 1-2 or 1-2-3-4-5... for example, if it is 1-2-3-4, how can it generate a multi-dimensional array: {code ...} thank you for not understanding how to quickly generate a multi-dimensional array from a one-dimensional array:
I have a field that is a string and the value may be 1 or 1-2 or 1-2-3-4-5...

For example, if it is 1-2-3-4, how can we generate a multi-dimensional array:

Array(    [1] => Array        (            [2] => Array                (                    [3] => Array                        (                            [4] => Array                                (                                )                        )                )        ))

Thanks

Reply content:

I have never understood how to quickly generate a multi-dimensional array from a one-dimensional array:
I have a field that is a string and the value may be 1 or 1-2 or 1-2-3-4-5...

For example, if it is 1-2-3-4, how can we generate a multi-dimensional array:

Array(    [1] => Array        (            [2] => Array                (                    [3] => Array                        (                            [4] => Array                                (                                )                        )                )        ))

Thanks

// from https://segmentfault.com/q/1010000004647414$str = '1-2-3-4';$arr = explode('-', $str);$last = null;while($last = array_pop($arr)) {    if(null != $last) {       array_push($arr,[array_pop($arr)=>$last]);    }    if(count($arr) <= 1) {        break;    } } print_r($arr[0]);/*Array(    [] => Array        (            [1] => Array                (                    [2] => Array                        (                            [3] => 4                        )                )        ))*/

Look at the matrix transpose function in the scientific computing package.

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.