The method I used was to separate the blocks by key, then to assign the block to other variables, and then do some action, which used a lot of for and foreach, and the amount of code is very large, so it was returned.
Through 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, as the result shows,
Requirement: To operate on an array that has been sorted by key, the same key is processed.
Hint: This is a very typical structure of mother-child table, that is to say, it is a combination of two tables, which can be processed into two arrays to facilitate the operation of the block in the array.
Array1:idkey
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-separated code like above
After that, it's very convenient to access the Temparray block data.
foreach ($tempArray as $row) {
array1[$row [' ID ']] = $row [' Key '];
array2[$row [' Key ']][] = $row;
}
Accessing and processing code
foreach ($array 1 as $ID => $Key) {
$this->dosomething ($ID);
Accessing the Temparray block array $array2[$Key]
$this->dosomething2 ($array 2[$Key]);
}