Php array merging and re-merging based on categories

Source: Internet
Author: User
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:

  1. $ Array1 = array (1 => '0 ');
  2. $ Array2 = array (1 => "data ");
  3. $ Result1 = $ array2 + $ array1;/* The result is a value of $ array2 */
  4. Print_r ($ result );
  5. $ Result = $ array1 + $ array2;/* The result is a value of $ array1 */
  6. Print_r ($ result );
  7. $ Result3 = array_merge ($ array2, $ array1);/* The result is the value of $ array2 and $ array1, and the key name is reassigned */
  8. Print_r ($ result3 );
  9. $ Result4 = array_merge ($ array1, $ array2);/* The result is $ array1 and $ array2, and the key name is reassigned */
  10. Print_r ($ result4 );
  11. Output result:
  12. Array ([1] => data)
  13. Array ([1] => 0)
  14. Array (
  15. [0] => data
  16. [1] => 0
  17. )
  18. Array
  19. (
  20. [0] => 0
  21. [1] => data
  22. )

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:

  1. $ Array1 = array ('asd '=> '0 ');
  2. $ Array2 = array ('asd '=> "data ");
  3. $ Result1 = $ array2 + $ array1;/* The result is a value of $ array2 */
  4. Print_r ($ result );
  5. $ Result = $ array1 + $ array2;/* The result is a value of $ array1 */
  6. Print_r ($ result );
  7. $ Result3 = array_merge ($ array2, $ array1);/* The result is $ array1 */
  8. Print_r ($ result3 );
  9. $ Result4 = array_merge ($ array1, $ array2);/* The result is $ array2 */
  10. Print_r ($ result4 );
  11. /*
  12. Output result:
  13. Array ([asd] => data)
  14. Array ([asd] => 0)
  15. Array ([asd] => 0)
  16. Array ([asd] => data)
  17. */

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:

  1. // Array to be reorganized
  2. $ Arrar = array ();
  3. $ Array [] = array ('itemid' => 110126866896, 'categoryid' => 111 );
  4. $ Array [] = array ('itemid' => 120126866896, 'categoryid' => 112 );
  5. $ Array [] = array ('itemid' => 130126866896, 'categoryid' => 113 );
  6. $ Array [] = array ('itemid' => 140126866896, 'categoryid' => 114 );
  7. $ Array [] = array ('itemid' => 150126866896, 'categoryid' => 115 );
  8. $ Array [] = array ('itemid' => 160126866896, 'categoryid' => 116 );
  9. $ Array [] = array ('itemid' => 170126866896, 'categoryid' => 117 );
  10. $ Array [] = array ('itemid' => 118126866896, 'categoryid' => 111 );
  11. $ Array [] = array ('itemid' => 121126866896, 'categoryid' => 112 );
  12. $ Array [] = array ('itemid' => 132126866896, 'categoryid' => 113 );
  13. $ Array [] = array ('itemid' => 143126866896, 'categoryid' => 114 );
  14. $ Array [] = array ('itemid' => 154126866896, 'categoryid' => 115 );
  15. $ Array [] = array ('itemid' => 165126866896, 'categoryid' => 116 );
  16. $ Array [] = array ('itemid' => 176126866896, 'categoryid' => 117 );
  17. // Array is reorganized by category
  18. $ NewArray = array ();
  19. Foreach ($ array as $ val ){
  20. $ NewArray [$ val ['categoryid'] [] = $ val;
  21. }
  22. // Delete the original array to release Space
  23. $ Array = null;
  24. Unset ($ array );
  25. Print_r ($ newArray );
  26. ?>

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.