数组合并然后排序

时间:2016-06-22 09:36:10

标签: php arrays sorting

我有2个阵列: $ all_projects $ project_jobs

他们看起来如下:

所有项目

array(2) {
  [0]=>
  object(stdClass)#319 (43) {
    ["project_id"]=>
    int(2)
    ["project_name"]=>
    string(13) "Consteel Time"
    ["project_number"]=>
    string(6) "2016-2"
    ["project_currency"]=>
    string(3) "gbp"
    ["project_lump_value"]=>
    string(4) "5000"
    ["project_hourly_rate"]=>
    string(5) "32.50"
    ["project_no_staff"]=>
    int(1)
    ["project_timeframe"]=>
    string(4) "3.42"
    ["project_value"]=>
    string(6) "153.85"
    ["project_colour"]=>
    NULL
    ["project_hours_submitted"]=>
    string(6) "103.85"
    ["leader_id"]=>
    int(3)
    ["company_id"]=>
    int(1)
    ["start_date"]=>
    string(19) "2016-03-29 00:00:00"
    ["end_date"]=>
    string(19) "2016-07-28 00:00:00"
    ["status"]=>
    string(4) "open"
    ["created_at"]=>
    string(19) "2016-03-10 12:19:45"
    ["updated_at"]=>
    string(19) "2016-03-10 12:58:22"
    ["deleted_at"]=>
    NULL
    ["user_id"]=>
    int(3)
    ["user_firstname"]=>
    string(4) "Adam"
    ["user_surname"]=>
    string(10) "Hutchinson"
    ["user_email"]=>
    string(29) "adam.hutchinson@mobilerock.it"
    ["user_password"]=>
    string(60) "$2y$10$brjhQpV.vH2I65B1NSiQOOuUl.nXtsHXCmmyZd89f.PCKPpXlnqkm"
    ["user_telephone"]=>
    string(2) "01"
    ["holiday_allocation"]=>
    NULL
    ["user_rate"]=>
    NULL
    ["user_number"]=>
    NULL
    ["payroll_no"]=>
    NULL
    ["role_id"]=>
    int(1)
    ["remember_token"]=>
    string(60) "QlIUYYO0ksoSzOdNlpUA3hkPnBm8K2aD1JJhMUiqGBGMaxsYowovbRlVORyU"
    ["forgotten_token"]=>
    NULL
    ["staff_no"]=>
    NULL
    ["staff_dept"]=>
    NULL
    ["last_login"]=>
    string(19) "2016-05-13 10:03:08"
    ["company_name"]=>
    string(15) "Koodoo Creative"
    ["company_address1"]=>
    string(2) "2 "
    ["company_address2"]=>
    string(13) "Parsons Court"
    ["company_city"]=>
    string(15) "Newton Aycliffe"
    ["company_telephone"]=>
    string(11) "08456445089"
    ["company_postcode"]=>
    string(8) " DL5 6ZE"
    ["company_lat"]=>
    string(14) "54.60049900487"
    ["company_long"]=>
    string(16) "-1.5687729520348"
  }
}

项目工作

array(3) {
  [0]=>
  object(stdClass)#321 (45) {
    ["project_job_id"]=>
    int(6)
    ["project_job_name"]=>
    string(25) "Consteel Time Project - 1"
    ["project_number"]=>
    string(6) "2016-2"
    ["project_job_number"]=>
    string(10) "2016-2-001"
    ["project_job_currency"]=>
    string(3) "gbp"
    ["project_job_lump_value"]=>
    string(3) "200"
    ["project_job_hourly_rate"]=>
    string(5) "32.50"
    ["project_job_no_staff"]=>
    int(5)
    ["project_job_timeframe"]=>
    string(4) "0.03"
    ["project_job_value"]=>
    string(4) "6.15"
    ["project_job_hours_submitted"]=>
    string(6) "-13.85"
    ["project_colour"]=>
    NULL
    ["leader_id"]=>
    int(3)
    ["company_id"]=>
    int(1)
    ["project_id"]=>
    int(2)
    ["start_date"]=>
    string(19) "2016-05-11 00:00:00"
    ["end_date"]=>
    string(19) "2016-05-20 00:00:00"
    ["status"]=>
    string(4) "open"
    ["created_at"]=>
    string(19) "2016-03-10 12:19:45"
    ["updated_at"]=>
    string(19) "2016-03-10 12:58:22"
    ["deleted_at"]=>
    NULL
    ["user_id"]=>
    int(3)
    ["user_firstname"]=>
    string(4) "Adam"
    ["user_surname"]=>
    string(10) "Hutchinson"
    ["user_email"]=>
    string(29) "adam.hutchinson@mobilerock.it"
    ["user_password"]=>
    string(60) "$2y$10$brjhQpV.vH2I65B1NSiQOOuUl.nXtsHXCmmyZd89f.PCKPpXlnqkm"
    ["user_telephone"]=>
    string(2) "01"
    ["holiday_allocation"]=>
    NULL
    ["user_rate"]=>
    NULL
    ["user_number"]=>
    NULL
    ["payroll_no"]=>
    NULL
    ["role_id"]=>
    int(1)
    ["remember_token"]=>
    string(60) "QlIUYYO0ksoSzOdNlpUA3hkPnBm8K2aD1JJhMUiqGBGMaxsYowovbRlVORyU"
    ["forgotten_token"]=>
    NULL
    ["staff_no"]=>
    NULL
    ["staff_dept"]=>
    NULL
    ["last_login"]=>
    string(19) "2016-05-13 10:03:08"
    ["company_name"]=>
    string(15) "Koodoo Creative"
    ["company_address1"]=>
    string(2) "2 "
    ["company_address2"]=>
    string(13) "Parsons Court"
    ["company_city"]=>
    string(15) "Newton Aycliffe"
    ["company_telephone"]=>
    string(11) "08456445089"
    ["company_postcode"]=>
    string(8) " DL5 6ZE"
    ["company_lat"]=>
    string(14) "54.60049900487"
    ["company_long"]=>
    string(16) "-1.5687729520348"
  }

然后我将两者合并在一起。我当时想做的是通过“project_id”对数组进行排序,这样当显示数据时,它们都会在一起。

我怎样才能做到这一点?

由于

3 个答案:

答案 0 :(得分:1)

$result = array_combine($array1, $array2);
asort($result);

/ *没有任何唯一键使用循环* /

$result = array();
foreach($array1as $key => $value){
$result[] = array('key' => $array2[$key], 'value' => $value);
}

// then sort by usort

答案 1 :(得分:1)

只需使用usort即可:

usort($mergedArrays, function ($a, $b) {
    return $a->project_id - $b->project_id;
});

答案 2 :(得分:1)

遵循三个简单的步骤

//both arrays will be merged including duplicates
$result = array_merge($all_projects, $project_jobs);

//duplicate objects will be removed
$result = array_map("unserialize", array_unique(array_map("serialize", $result)));

//array is sorted on the bases of id
sort( $result );