


Detailed explanation of how to group two-dimensional arrays according to key in PHP
This article mainly introduces the relevant information of PHP grouping two-dimensional arrays according to key. It is very good and has reference value. Friends who need it can refer to it
We often get a two-dimensional array. , you will find that the results are somewhat different from what you want. You may need to group the array according to a certain field in the two-dimensional array.
Let’s first look at the following array,
Array ( [0] => Array ( [id] => 1 [wo_id] => 2 [evaluate_id] => 1 [type] => 分组1 [ctime] => 2016-12-02 11:39:34 ) [1] => Array ( [id] => 2 [wo_id] => 31 [evaluate_id] => 2 [type] => 分组1 [ctime] => 2016-12-12 11:39:50 ) [2] => Array ( [id] => 3 [wo_id] => 31 [evaluate_id] => 4 [type] => 分组2 [ctime] => 2016-12-02 11:40:01 ) [3] => Array ( [id] => 4 [wo_id] => 2 [evaluate_id] => 5 [type] => 分组2 [ctime] => 2016-12-07 10:21:40 ) [4] => Array ( [id] => 5 [wo_id] => 2 [evaluate_id] => 3 [type] => 分组2 [ctime] => 2016-12-07 10:21:51 ) [5] => Array ( [id] => 6 [wo_id] => 2 [evaluate_id] => 2 [type] => 分组3 [ctime] => 2016-12-07 10:21:56 ) [6] => Array ( [id] => 7 [wo_id] => 4 [evaluate_id] => 2 [type] => 分组3 [ctime] => 2016-12-07 10:22:10 ) )
Here is a very typical example , if I want to process the data in the array separately according to type (grouping), if not grouped, the loop may be very redundant, so I have the following grouping function:
public static function array_group_by($arr, $key) { $grouped = []; foreach ($arr as $value) { $grouped[$value[$key]][] = $value; } // Recursively build a nested grouping if more parameters are supplied // Each grouped array value is grouped according to the next sequential key if (func_num_args() > 2) { $args = func_get_args(); foreach ($grouped as $key => $value) { $parms = array_merge([$value], array_slice($args, 2, func_num_args())); $grouped[$key] = call_user_func_array('array_group_by', $parms); } } return $grouped; }
$arr: two-dimensional array
$key: the key to be grouped
The result is:
Array ( [分组2] => Array ( [0] => Array ( [id] => 1 [wo_id] => 2 [evaluate_id] => 1 [ctime] => 2016-12-02 11:39:34 [type] => 分组2 ) [1] => Array ( [id] => 2 [wo_id] => 31 [evaluate_id] => 2 [ctime] => 2016-12-12 11:39:50 [type] => 分组2 ) [2] => Array ( [id] => 3 [wo_id] => 31 [evaluate_id] => 4 [ctime] => 2016-12-02 11:40:01 [type] => 分组2 ) ) [分组1] => Array ( [0] => Array ( [id] => 4 [wo_id] => 2 [evaluate_id] => 5 [ctime] => 2016-12-07 10:21:40 [type] => 分组1 ) [1] => Array ( [id] => 5 [wo_id] => 2 [evaluate_id] => 3 [ctime] => 2016-12-07 10:21:51 [type] => 分组1 ) ) )
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP filter two-dimensional array and three-dimensional array, php two-dimensional array array
PHP filtering two-dimensional arrays and three-dimensional arrays, php two Dimension array array
The above is the detailed content of Detailed explanation of how to group two-dimensional arrays according to key in PHP. For more information, please follow other related articles on the PHP Chinese website!

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools
