Find a way to solve PHP code

Source: Internet
Author: User
Ask for a PHP code
There is a need to combine multiple data into one.
$data = Array ();
$data [] = array ("id" = "123", "name" = "Money", "before_save" = "ten", "After_save" and "" "," create_date "= > "2014-10-23");
$data [] = array ("id" = "123", "name" = "Money", "before_save" = "+", "After_save" and "+", "create_date" = > "2014-10-24");
$data [] = array ("id" = "123", "name" = "Money", "before_save" = "+", "After_save" and "" "," create_date "= > "2014-10-25");

$data [] = array ("id" = "123", "name" = "duration", "Before_save" and "Ten", "After_save" and "+", "create_date "=" 2014-10-23 ");
$data [] = array ("id" = "123", "name" = "duration", "before_save" = "+", "After_save" and "a", "create_date "=" 2014-10-24 ");


$data [] = array ("id" = "456", "name" and "Money", "before_save" = "$", "After_save" and "Max", "create_date" = > "2014-10-26");
$data [] = array ("id" = "456", "name" = "Money", "before_save" = "" "," After_save "and" "", "create_date" = > "2014-10-27");
$data [] = array ("id" = "456", "name" = "Money", "before_save" = "" "," After_save "and" + "," create_date " = "2014-10-28");

$data [] = array ("id" = "456", "name" = "duration", "Before_save" and "a", "After_save" and "a", "create_date "=" 2014-10-26 ");
$data [] = array ("id" = "456", "name" = "duration", "Before_save" and "Create_date", "after_save" = "" "=" 2014-10-27 ");
$data [] = array ("id" = "456", "name" = "duration", "before_save" = "a", "After_save" and "" "," Create_dat E "=" 2014-10-28 ");

Merge the above ID and name into one piece of data.
The consolidation rules are:
The value of Before_save is the Before_save value of the smallest create_date.
The value of After_save is the After_save value of the create_date maximum.
Maximum value of create_date

For example, the first three data results need to be
$data [] = array ("id" = "123", "name" = "Money", "before_save" = "ten", "After_save" and "" "," create_date "= > "2014-10-25");


The above results will result in a total of 4 data, respectively
$returndata [0] = array ("id" = "123", "name" = "Money", "before_save" = "ten", "After_save" and "" "," Create_ Date "=" 2014-10-25 ");
$returndata [1] = array ("id" = "123", "name" = "duration", "before_save" = "ten", "After_save" and "" "," Crea Te_date "=" 2014-10-24 ");
$returndata [2] = array ("id" = "456", "name" and "Money", "before_save" = "$", "After_save" and "+", "create _date "=" 2014-10-28 ");
$returndata [3] = array ("id" = "456", "name" = "duration", "Before_save" and "a", "After_save" and "+", "cre Ate_date "=" 2014-10-28 ");


Little brother just learn php not long, not too familiar, ask the great God help
------to solve the idea----------------------
This post was last edited by xuzuning on 2014-11-02 18:55:54

$res = Array ();
foreach ($data as $t) $res [$t [' id ']][$t [' name ']][$t [' create_date ']] = $t;

$returndata = Array ();
foreach ($res as $t) {
foreach ($t as $r) {
Ksort ($R);
$v = Array_pop ($r);
$p = Array_shift ($r);
$v [' before_save '] = $p [' Before_save '];
$returndata [] = $v;
}
}
Print_r ($returndata);
  • 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.