dpepbjp126917 2016-06-22 09:36
浏览 25
已采纳

数组合并然后排序

I have 2 arrays : $all_projects and $project_jobs

They look as follows :

All Projects

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"
  }
}

Project Jobs

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"
  }

I then merge the two together. What I'd then like to do is sort the array by the "project_id" so that when showing the data back out they all sit together.

How can I achieve this?

Thanks

  • 写回答

3条回答 默认 最新

  • duan2477 2016-06-22 09:57
    关注

    Follow Three simple steps

    //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 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测