PHP multi-dimensional array generation unified subscript Index Method

Source: Internet
Author: User
The following code is required: $ paramsArray ([tab1] Array ([server] Array ([0] 137 [1] 122) to generate the following Array: $ paramsArray ([tab1] [server] [0] 137, [tab1] [server] [1] 122); note that, is to integrate the subscript in the multi-dimensional array into a unified index

The following code is required: $ params = Array ([tab1] = Array ([server] = Array ([0] = 137 [1] = 122); generates the following Array: $ params = Array ([tab1] [server] [0] = 137, [tab1] [server] [1] = 122); note that, is to integrate the subscript in the multi-dimensional array into a unified index

The following code is required:

$ Params =
Array
(
[Tab1] => Array
(
[Server] => Array
(
[0] = & gt; 137
[1] => 122.
)
)
);

Generate the following array:
$ Params =
Array (
'[Tab1] [server] [0]' = 137,
'[Tab1] [server] [1]' = 122
);

Note that the subscript in the multi-dimensional array is integrated into a unified index.
I wrote a test code as follows to see if you have any better suggestions and ideas: EaglePHP

Source code and demo:Source code source demonstration Source

$params = array('tab1'=>array('server1'=>array(137, 122),  'server2'=>array(100,200)),'tab2'=>array('server3'=>array(400, 500),  'server4'=>array(600, 700),   'aaa'=>array('bbb'=>array(1,2,3))),'name' => 'dimain','abc' => array(4,5,6=>array('apple', 'lizi'))  );  function test(&$params, &$tmpKeyArr, &$data){$i = 0;foreach ($params as $k=>&$v){$i++;if(is_array($v)) {$tmpKeyArr[] = '['.$k.']';test($v, $tmpKeyArr, $data);}else{$data[implode($tmpKeyArr, '').'['.$k.']'] = $v;}if(count($params) == $i) array_pop($tmpKeyArr);}}$tmpKeyArr = array();$data = array();test($params, $tmpKeyArr, $data);echo '
';print_r($data);print_r($params);echo '
';

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.