PHP array grouping and sorting program code-PHP source code

Source: Internet
Author: User
In php, you can use sort for data sorting. However, if you want to sort data by array group, you can use the array_multisort function or multiple repeated traversal functions. The following is an example. In php, you can use sort for data sorting. However, if you want to sort data by array group, you can use the array_multisort function or multiple repeated traversal functions. The following is an example.

Script ec (2); script

PHP array. The content in the array is roughly as follows:

The Code is as follows:

$ List = array (
Array (2, 3, 5 ),
Array (2, 5, 24 ),
Array (3, 8, 6 ),
Array (3, 2, 10 ),
Array (4,7, 20 ),
Array (4,1, 15 ),
Array (6, 4, 10 ),
Array (7,9, 20 ),
);

For ease of expression, I name three columns of numbers and three columns of ABC respectively.

Requirement: columns A are sorted by default. If Column A is the same, columns C are arranged in reverse order with the same elements. Column B is actually not involved in sorting, but it is useful in practical use, so I also wrote it out.

Method 1:

The Code is as follows:

$ A = $ c = array ();
Foreach ($ list as $ val ){
$ A [] = $ val [0]; // column
$ C [] = $ val [2]; // column c
}
// Install column a in ascending order, and then install Column B in descending order, similar to SQL, orderby a asc, B desc
Array_multisort ($ a, SORT_ASC, $ c, SORT_DESC, $ list );
Print_r ($ list );

Method 2:

The Code is as follows:

For ($ j = 0; $ j For ($ I = count ($ list)-1; $ I> $ j; $ I --){
If ($ list [$ I] [0] = $ list [$ I-1] [0] & $ list [$ I] [2]> $ list [$ I-1] [2])
List ($ list [$ I], $ list [$ I-1]) = array ($ list [$ I-1], $ list [$ I]);
}
}

Example 2

For example, if the following data is found in the database, it is a two-dimensional array.
[0] => Array ([trans_lang_id] => 1 [country_id] => 1 [trans_origin_id] => 1 [page_id] => 1 [trans_content] => test1
[1] => Array ([trans_lang_id] => 2 [country_id] => 1 [trans_origin_id] => 2 [page_id] => 1 [trans_content] => test2
[2] => Array ([trans_lang_id] => 3 [country_id] => 1 [trans_origin_id] => 3 [page_id] => 1 [trans_content] => test3
[3] => Array ([trans_lang_id] => 4 [country_id] => 1 [trans_origin_id] => 4 [page_id] => 1 [trans_content] => test4
[4] => Array ([trans_lang_id] => 6 [country_id] => 2 [trans_origin_id] => 1 [page_id] => 2 [trans_content] => test5

How to group data by country_id and page_id into multiple arrays?
That is, the data with the same country_id and page_id becomes a new array. Because such data will be output to the same csv file.
For example, the file name is zh_CN_1.csv.

The Code is as follows:

Array Translates:
US_en: Array (
[1] => Array (
[0] => Array ([0] => test [1] => test)
[1] => Array ([0] => test [1] => test)
[2] => Array ([0] => test [1] => test)
[3] => Array ([0] => test [1] => test)
)
)

CN_zh: Array (
[2] => Array (
[0] => Array ([0] => There are no user contributed notes for this page. [1] => this belong to another country)
)
)

Foreach ($ translates as $ translate ){
$ Data [$ translate ["language_code"]. "_". $ translate ['short _ name'] [$ translate ['page _ id'] [] = array ($ translate ['content'], $ translate ['Trans _ content']);
}
Foreach ($ data as $ locale_key => $ item ){
Foreach ($ item as $ page_key => $ csvContent ){
$ FileName = $ locale_key. "_". $ page_key. ". csv ";
$ TranslationFile-> generateCSV ($ fileName, $ csvContent );
}

In this case, the output CSV is like the following structure:
Zh_CN_1.csv
Zh_CN_2.csv
En_US_1.csv
En_US_2.csv

Each csv may contain multiple pieces of data, that is, the data with the same country language and page_id is stored in the same csv.

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.