An upgraded version of array_column, the record set processing function.

Source: Internet
Author: User
An upgraded version of array_column, the record set processing function.
Nice: Simplified operation, batch processing of array_walk, reference, and low internal consumption.

Application scenario: mainly used to process record sets (two-dimensional arrays)
1. integrate the result set (one-dimensional array, such as ids: [,]. list of winning usernames: ['xiong Ming', 'lijia ', 'erlang Shenzhen'])
2. key-value pairs (one-dimensional arrays, such as tags: array ('mysql' => 'url1', 'php' => 'url2'])
3. cached file data (id => info)

Note: php5.5 is used as a built-in function. Note the naming conflict before use.

From (EQPHP tool class): https://github.com/eqphp/framework/blob/master/www/class/fun.php

Demo: http://www.eqphp.com/blog/list/

  1. // Process the record set (php5.5 built-in)
  2. Static function array_column ($ data, $ key = 'id', $ column = null ){
  3. $ Buffer = null;
  4. If ($ column ){
  5. // K => v
  6. If (strpos ($ column, ',') = false ){
  7. Foreach ($ data as $ value ){
  8. $ Buffer [$ value [$ key] = $ value [$ column];
  9. }
  10. Return $ buffer;
  11. }
  12. // K = arr
  13. $ Field = explode (',', $ column );
  14. Foreach ($ data as $ value ){
  15. $ Id = $ value [$ key];
  16. Array_walk ($ value, function ($ v, $ k) use (& $ value, $ field ){
  17. If (! In_array ($ k, $ field) unset ($ value [$ k]);
  18. });
  19. $ Buffer [$ id] = $ value;
  20. }
  21. Return $ buffer;
  22. }
  23. // Id_arr
  24. Foreach ($ data as $ value ){
  25. $ Buffer [] = $ value [$ key];
  26. }
  27. Return $ buffer;
  28. }
  29. // Usage demo:
  30. // Original record set:
  31. $ Data = array (
  32. Array ('id' => 1, 'name' => 'dubox', 'age' => 25, 'girlfriend' => 'soda green '),
  33. Array ('id' => 4, 'name' => 'Xiao chun', 'age' => 21, 'girlfriend' => 'Unknown '),
  34. Array ('id' => 9, 'name' => 'id', 'age' => 71, 'girlfriend' => 'widow '),
  35. );
  36. $ Ids = array_column ($ data, 'id ');
  37. // Out: array (1, 4, 9 );
  38. $ Info = array_column ($ data, 'name', 'girlfriend ');
  39. // Out: array ('dubox' => 'soda Green', 'Little Chung' => 'Don't know ', 'id' => 'widow ');
  40. $ Cache = array_column ($ data, 'id', 'name, age, Girlfriend ');
  41. // Out: array (
  42. 1 => array ('name' => 'dubox', 'age' => 25, 'girlfriend' => 'soda green '),
  43. 4 => array ('name' => 'Spring sub', 'age' => 21, 'girlfriend' => 'Unknown '),
  44. 9 => array ('name' => 'id', 'age' => 71, 'girlfriend' => 'widow '),
  45. );

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.