PHP array content does not combine the Arrangement Algorithm repeatedly, and php array Arrangement Algorithm

Source: Internet
Author: User

PHP array content does not combine the Arrangement Algorithm repeatedly, and php array Arrangement Algorithm

/* Param: $ arrs two-dimensional array
GetArrSet (array (),...))
Arrays do not repeat the set
*/
Function getArrSet ($ arrs, $ _ current_index =-1)
{
// Total group
Static $ _ total_arr;
// Total number of subscripts
Static $ _ total_arr_index;
// Input array Length
Static $ _ total_count;
// Concatenate an array temporarily
Static $ _ temp_arr;

// Enter the first layer of the input array, clear the static array, and initialize the length of the input Array
If ($ _ current_index <0)
{
$ _ Total_arr = array ();
$ _ Total_arr_index = 0;
$ _ Temp_arr = array ();
$ _ Total_count = count ($ arrs)-1;
GetArrSet ($ arrs, 0 );
}
Else
{
// Number of layers in the loop $ _ current_index
Foreach ($ arrs [$ _ current_index] as $ v)
{
// If the current loop array is less than the input array Length
If ($ _ current_index <$ _ total_count)
{
// Add the value of the current array to the temporary Array
$ _ Temp_arr [$ _ current_index] = $ v;
// Continue to loop the next Array
GetArrSet ($ arrs, $ _ current_index + 1 );

}
// If the current loop array is equal to the input array length (this array is the final array)
Else if ($ _ current_index === _ total_count)
{
// Add the value of the current array to the temporary Array
$ _ Temp_arr [$ _ current_index] = $ v;
// Add the temporary array to the total number Group
$ _ Total_arr [$ _ total_arr_index] = $ _ temp_arr;
// Total number of subscripts + 1
$ _ Total_arr_index ++;
}

}
}

Return $ _ total_arr;
}

/************* TEST **************/
$ Arr = array (
Array ('red', 'White '),
Array ('red', 'Blue ', 'black '),
Array ('brown ', 'Gray', 'purple '),
Array ('green', 'cyan ', 'baby Blue ')
);
Echo "<pre> ";
Print_r (getArrSet ($ arr ));

Function zuhe ($ arr ){
If (count ($ arr)> = 2 ){
$ Tmparr = array ();
$ Arr1 = array_shift ($ arr );
$ Arr2 = array_shift ($ arr );
Foreach ($ arr1 as $ k1 => $ v1 ){
Foreach ($ arr2 as $ k2 => $ v2 ){
$ Tmparr [] = $ v1. $ v2;
}}
Array_unshift ($ arr, $ tmparr );
$ Arr = zuhe ($ arr );
} Else {
Return $ arr;
}
Return $ arr;
}
$ Arr = array (
Array (1, 2 ),
Array ('A', 'B '),
Array ('C', 'D ')
);
Print_r (zuhe ($ arr); exit;

Reference from https://www.cnblogs.com/GaZeon/p/5602049.html

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.