Ascending and Descending dimension processing of PHP arrays

Source: Internet
Author: User

Recently learned a bit of PHP array of ascending and descending dimension, nonsense less, on the demo.

Suppose there is an array before descending, as follows $array = Array (' a ' = = = Array (' b ' = = 1, ' c ' = = = Array (' d ' = = 2, ' E ' + 3) '), ' f ' = = 4, ' g ' = = AR Ray (' H ' =>5));

After dimensionality reduction, the following:

Descending dimension $array = Array (    ' 0 ' = = a->b->1,    ' 1 ' = = a->c->d->2,    ' 2 ' = = A->c->e->3 ,    ' 3 ' = f->4,    ' 4 ' = g->h->5);

The following function is implemented:

/** * dimension arrays are reduced to a one-dimensional array, recursive implementation * @param unknown $arr * @param string $delimiter * @param string $key * @return boolean|  Ambigous <multitype:string, multitype:> */function multitosingle ($arr, $delimiter = ', $key = ') {$resultAry = Array (); if (! ( Is_array ($arr) && count ($arr) >0) {return false;} foreach ($arr as $k = = $val) {$newKey = Trim ($key. $k. $delimiter); if (Is_array ($val) && count ($val) >0) {$re Sultary = Array_merge ($resultAry, Multitosingle ($val, $delimiter, $newKey));} else {$resultAry [] = $newKey. $val;}} return $resultAry;}

The array has a descending must have ascending, ascending dimension comes:

/** * Wi Cheng a one-dimensional array to a multidimensional array * @param unknown $arr * @param string $delimiter * @return Multitype: */function singletomulti ($arr, $d Elimiter = ') {$endAry = array (); foreach ($arr as $key = = $val) {$sAry = explode ($delimiter, $val); $len = count ($sAr y); $ary = Array (); for ($i = $len-2; $i >-1; $i-) {if ($i = = $len-2) {$ary [$sAry [$i]] = $sAry [$i +1];} else {$ary = array ( $sAry [$i] = $ary);}} $endAry = Array_merge_recursive ($endAry, $ary);} return $endAry;}

The realization of the array of ascending and descending dimension, perhaps the implementation of the way is not good enough, please criticize correct!

Ascending and Descending dimension processing of PHP arrays

Related Article

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.