In php, there are many ways to merge arrays in a simple way, but today I want to merge arrays based on the array ID and then re-merge the arrays based on categories. To merge the simplest arrays, we only need
In php, there are many ways to merge arrays in a simple way, but today I want to merge arrays based on the array ID and then re-merge the arrays based on categories.
We only need to use array_merge to merge the simplest array. array_merge () combines two or more arrays. the values in an array are appended to the values of the previous array, returns an array of results.
When the array KEY name is a numeric KEY, when the two arrays to be merged have a numeric KEY with the same name, using array_merge () will not overwrite the original value, when "+" is used to merge arrays, the first value will be returned as the final result, and the values of the following arrays with the same key name will be discarded (note: the example code is as follows:
- $ Array1 = array (1 => '0 ');
- $ Array2 = array (1 => "data ");
- $ Result1 = $ array2 + $ array1;/* The result is a value of $ array2 */
- Print_r ($ result );
- $ Result = $ array1 + $ array2;/* The result is a value of $ array1 */
- Print_r ($ result );
- $ Result3 = array_merge ($ array2, $ array1);/* The result is the value of $ array2 and $ array1, and the key name is reassigned */
- Print_r ($ result3 );
- $ Result4 = array_merge ($ array1, $ array2);/* The result is $ array1 and $ array2, and the key name is reassigned */
- Print_r ($ result4 );
- Output result:
- Array ([1] => data)
- Array ([1] => 0)
- Array (
- [0] => data
- [1] => 0
- )
- Array
- (
- [0] => 0
- [1] => data
- )
When the same array key name is a character, the "+" operator is the same as the key name is a number, but array_merge () will overwrite the value of the previous same key name.
The sample code is as follows:
- $ Array1 = array ('asd '=> '0 ');
- $ Array2 = array ('asd '=> "data ");
- $ Result1 = $ array2 + $ array1;/* The result is a value of $ array2 */
- Print_r ($ result );
- $ Result = $ array1 + $ array2;/* The result is a value of $ array1 */
- Print_r ($ result );
- $ Result3 = array_merge ($ array2, $ array1);/* The result is $ array1 */
- Print_r ($ result3 );
- $ Result4 = array_merge ($ array1, $ array2);/* The result is $ array2 */
- Print_r ($ result4 );
- /*
- Output result:
- Array ([asd] => data)
- Array ([asd] => 0)
- Array ([asd] => 0)
- Array ([asd] => data)
- */
After talking about so many issues, the following is what I want to introduce to you. for example, reorganize the array based on the classification field. the code is as follows:
-
- // Array to be reorganized
- $ Arrar = array ();
- $ Array [] = array ('itemid' => 110126866896, 'categoryid' => 111 );
- $ Array [] = array ('itemid' => 120126866896, 'categoryid' => 112 );
- $ Array [] = array ('itemid' => 130126866896, 'categoryid' => 113 );
- $ Array [] = array ('itemid' => 140126866896, 'categoryid' => 114 );
- $ Array [] = array ('itemid' => 150126866896, 'categoryid' => 115 );
- $ Array [] = array ('itemid' => 160126866896, 'categoryid' => 116 );
- $ Array [] = array ('itemid' => 170126866896, 'categoryid' => 117 );
- $ Array [] = array ('itemid' => 118126866896, 'categoryid' => 111 );
- $ Array [] = array ('itemid' => 121126866896, 'categoryid' => 112 );
- $ Array [] = array ('itemid' => 132126866896, 'categoryid' => 113 );
- $ Array [] = array ('itemid' => 143126866896, 'categoryid' => 114 );
- $ Array [] = array ('itemid' => 154126866896, 'categoryid' => 115 );
- $ Array [] = array ('itemid' => 165126866896, 'categoryid' => 116 );
- $ Array [] = array ('itemid' => 176126866896, 'categoryid' => 117 );
- // Array is reorganized by category
- $ NewArray = array ();
- Foreach ($ array as $ val ){
- $ NewArray [$ val ['categoryid'] [] = $ val;
- }
- // Delete the original array to release Space
- $ Array = null;
- Unset ($ array );
- Print_r ($ newArray );
- ?>