A classic example of PHP code optimization _php Tutorial

Source: Internet
Author: User
I used the method of distinguishing blocks by key, then assigning the block to other variables, and then doing some work, so I used a lot of for and foreach, and the code was very large, so I was back.

After the above guidance, found really good simple, and now share with you.

Id
FIELD1
FIELD2 FIELD3 FIELD4 Key
1
*** *** *** *** Meat1
2
*** *** *** *** Meat1
3
*** *** *** *** Meat1
4 *** *** *** *** Meat1
5
*** *** *** *** Fruit2
6 *** *** *** *** Fruit2
7
*** *** *** *** Fruit2
8
*** *** *** *** Fruit2
9
*** *** *** *** Fruit2
10
*** *** *** *** Food3
11
*** *** *** *** Food3

Now the result is as shown above

Requirement: To operate on an array that has been sorted by key, the item of the same key is processed.

Hint: This is a very typical parent-child table structure, that is to say, it is a combination of two tables, can be processed into two arrays, convenient array in the face of the operation of the block
array1:id| Key

Id
Key
1
Meat1
2
Meat1
3
Meat1
4 Meat1
5
Fruit2
6 Fruit2
7
Fruit2
8
Fruit2
9
Fruit2
10
Food3
11
Food3

Array2:key = Array (id,field1,field2,field3,field4,field5,key)


Id
FIELD1
FIELD2 FIELD3 FIELD4 Key
Meat1=>
1
*** *** *** *** Meat1
2
*** *** *** *** Meat1
3
*** *** *** *** Meat1
4 *** *** *** *** Meat1
Fruit2=> 5
*** *** *** *** Fruit2
6 *** *** *** *** Fruit2
7
*** *** *** *** Fruit2
8
*** *** *** *** Fruit2
9
*** *** *** *** Fruit2
Food3=> 10
*** *** *** *** Food3
11
*** *** *** *** Food3

Implementing an array separation code like above

After that, it is very convenient to access the block data of the Temparray.

foreach ($tempArray as $row) {

array1[$row [' ID ']] = $row [' Key '];

array2[$row [' Key ']][] = $row;

}

Accessing and handling code

foreach ($array 1 as $ID = = $Key) {

$this->dosomething ($ID);

  Access Temparray block array $array2[$Key]

$this->dosomething2 ($array 2[$Key]);

}

http://www.bkjia.com/PHPjc/323010.html www.bkjia.com true http://www.bkjia.com/PHPjc/323010.html techarticle the way I use it is to distinguish blocks by key, and then to assign the blocks to other variables, and then perform some operations, so that a lot of for and foreach are used, and the amount of code is very large, so it's back ...

  • 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.