PHP merges a field with the same array and reorganizes the data algorithm

Source: Internet
Author: User
Tags vars

  1. $array Reorganization of an array:
  2. $array = Array (
  3. Array (' id ' =>1,' name ' = 'aa ',' pwd ' = ' + '),
  4. Array (' id ' =>1,' name ' = = 'bb ',' pwd ' = ' + '),
  5. Array (' id ' =>1,' name ' = 'saa ',' pwd ' = ' ") ',
  6. Array (' id ' =>2,' name ' = 'cc ',' pwd ' = ' + ')
  7. );
  8. The implementation code is as follows:
  9. $tmpArray = Array ();
  10. foreach ($array as $row) {
  11. $key = $row [' id '];
  12. if (array_key_exists ($key, $tmpArray)) {
  13. //$tmpArray [$key] [' pwd '] = $tmpArray [$key] [' pwd ']. ‘,‘ .  $row [' pwd ']; In this sentence, the following conditional statements about PWD are removed to implement their string form
  14. if (is_array ($tmpArray [$key] [' name ']) {
  15. $tmpArray [$key] [' name '] [] = $row [' name '];
  16. } Else {
  17. $tmpArray [$key] [' name '] = Array ($tmpArray [$key] [' name '] , $row [' name ']);
  18. }
  19. if (is_array ($tmpArray [$key] [' pwd ']) {
  20. $tmpArray [$key] [' pwd '] [] = $row [' pwd '];
  21. } Else {
  22. $tmpArray [$key] [' pwd '] = Array ($tmpArray [$key [' pwd '], $row [' pwd ']);
  23. }
  24. } Else {
  25. $tmpArray [$key] = Array (' id ' = +$row [' id '],' name ' = =Array ($row [' name ' ]),' pwd ' = =Array ($row [' pwd ']); //This sentence to note that can be set
  26. //$tmpArray [$key] = $row;//You may replace the above sentence when there are fields added
  27. }
  28. }
  29. echo "<pre>";
  30. Var_dump ($tmpArray);
  31. Printing results are:
  32. Array (size=2)
  33. 1 =
  34. Array (size=3)
  35. ' id ' = int 1
  36. ' name ' + =
  37. Array (size=3)
  38. 0 = string ' AA ' (length=2)
  39. 1 = string ' BB ' (length=2)
  40. 2 = string ' saa ' (length=3)
  41. ' pwd ' =
  42. Array (size=3)
  43. 0 = string ' a ' (length=2)
  44. 1 = string ' All ' (length=2)
  45. 2 = string ' "" (length=2)
  46. 2 =
  47. Array (size=3)
  48. ' id ' = int 2
  49. ' name ' + =
  50. Array (size=1)
  51. 0 = string ' cc ' (length=2)
  52. ' pwd ' =
  53. Array (size=1)
  54. 0 = string ' + ' (length=2)
  1. $field is a one-dimensional array of numbers, $field [0] is a single field for grouping.
  2. public function combine ($array,$field) {
  3. $tmpArray = Array ();
  4. $distinct _field = $field [0];
  5. unset ($field [0]);
  6. foreach ($array as $row) {
  7. $key = $row [$distinct _field];
  8. if (array_key_exists ($key, $tmpArray)) {
  9. foreach ($field as $value) {
  10. if (is_array ($tmpArray [$key] [$value])) {
  11. $tmpArray [$key] [$value] [] = $row [$value];
  12. } Else {
  13. $tmpArray [$key] [$value] = Array ($tmpArray [$key] [$value], $row [$value]);
  14. }
  15. }
  16. } Else {
  17. $tmpArray [$key] [$distinct _field] = $row [$distinct _field];
  18. foreach ($field as $value) {
  19. $tmpArray [$key] [$value] = Array ($row [$value]);
  20. }
  21. }
  22. }
  23. return $tmpArray;
  24. }

PHP merges a field with the same array and reorganizes the data algorithm

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.