过滤数组中的常见数据

时间:2015-03-17 12:20:36

标签: php

嗨我是php的新手,我的数组中包含一些常见数据,我尝试了很多代码,搜索的内容也越来越多,但我无法做到这一点请帮助我谢谢。

Array(
[0] => stdClass Object
    (
        [user_id] => 52
        [owner] => abc@gmail.com
        [sender] => Steve
        [alert_id] => 18
        [alert_community_id] => 1
        [alert_user_id] => 52
        [alert_text] => asdfasdf
        [alert_url] => asdfasdf-1
        [alert_note] => 
        [alert_source_proof] => asdfasdfasf
        [alert_proof_image] => 
        [is_time_critical] => 0
        [alert_reported_ip] => 80.255.4.52
        [alert_created_time] => 2015-03-15 15:25:20
        [direct_sending] => 0
        [ask_to_additional_reviwer] => 0
        [ask_additional_reviewer_at] => 
        [alert_reviewers] => 
        [alert_confirmed_by] => 56
        [confirmed_time] => 2015-03-15 19:26:35
        [alert_decliened_by] => 
        [decliened_time] => 
        [alert_status] => 1
        [community_id] => 1
        [hashtag] => #Nick
        [community_title] => Test community Nick #Nick asdfsa ds adsfa sd asdf asd asdf asf asdfa sdf
        [community_url] => test-community-nick-nick-asdfsa-ds-adsfa-sd-asdf-asd-asdf-as
        [user_type] => Apprentice
        [user_type_image] => Apprentice1416902127.png
    )

[1] => stdClass Object
    (
        [user_id] => 55
        [owner] => abc@gmail.com
        [sender] => Steve
        [alert_id] => 17
        [alert_community_id] => 1
        [alert_user_id] => 55
        [alert_text] => asdfasdf
        [alert_url] => asdfasdf
        [alert_note] => 
        [alert_source_proof] => asdfasdfsadf
        [alert_proof_image] => 
        [is_time_critical] => 0
        [alert_reported_ip] => 80.255.4.52
        [alert_created_time] => 2015-03-15 15:20:34
        [direct_sending] => 0
        [ask_to_additional_reviwer] => 0
        [ask_additional_reviewer_at] => 
        [alert_reviewers] => 
        [alert_confirmed_by] => 54
        [confirmed_time] => 2015-03-15 19:21:19
        [alert_decliened_by] => 
        [decliened_time] => 
        [alert_status] => 1
        [community_id] => 1
        [hashtag] => #Nick
        [community_title] => Test community Nick #Nick asdfsa ds adsfa sd asdf asd asdf asf asdfa sdf
        [community_url] => test-community-nick-nick-asdfsa-ds-adsfa-sd-asdf-asd-asdf-as
        [user_type] => Rising star
        [user_type_image] => RisingStar141690.png
    )

[2] => stdClass Object
    (
        [user_id] => 55
        [owner] => monadshare@gmail.com
        [sender] => chris
        [alert_id] => 17
        [alert_community_id] => 1
        [alert_user_id] => 55
        [alert_text] => asdfasdf
        [alert_url] => asdfasdf
        [alert_note] => 
        [alert_source_proof] => asdfasdfsadf
        [alert_proof_image] => 
        [is_time_critical] => 0
        [alert_reported_ip] => 80.255.4.52
        [alert_created_time] => 2015-03-15 15:20:34
        [direct_sending] => 0
        [ask_to_additional_reviwer] => 0
        [ask_additional_reviewer_at] => 
        [alert_reviewers] => 
        [alert_confirmed_by] => 54
        [confirmed_time] => 2015-03-15 19:21:19
        [alert_decliened_by] => 
        [decliened_time] => 
        [alert_status] => 1
        [community_id] => 1
        [hashtag] => #Nick
        [community_title] => Test community Nick #Nick asdfsa ds adsfa sd asdf asd asdf asf asdfa sdf
        [community_url] => test-community-nick-nick-asdfsa-ds-adsfa-sd-asdf-asd-asdf-as
        [user_type] => Rising star
        [user_type_image] => RisingStar141690.png
    )

[3] => stdClass Object
    (
        [user_id] => 55
        [owner] => monadshare@gmail.com
        [sender] => John
        [alert_id] => 16
        [alert_community_id] => 1
        [alert_user_id] => 55
        [alert_text] => asdf
        [alert_url] => asdf-1
        [alert_note] => 
        [alert_source_proof] => asfd
        [alert_proof_image] => 
        [is_time_critical] => 0
        [alert_reported_ip] => 80.255.4.52
        [alert_created_time] => 2015-03-15 15:08:48
        [direct_sending] => 0
        [ask_to_additional_reviwer] => 0
        [ask_additional_reviewer_at] => 
        [alert_reviewers] => 
        [alert_confirmed_by] => 56
        [confirmed_time] => 2015-03-15 19:09:16
        [alert_decliened_by] => 
        [decliened_time] => 
        [alert_status] => 1
        [community_id] => 1
        [hashtag] => #Nick
        [community_title] => Test community Nick #Nick asdfsa ds adsfa sd asdf asd asdf asf asdfa sdf
        [community_url] => test-community-nick-nick-asdfsa-ds-adsfa-sd-asdf-asd-asdf-as
        [user_type] => Rising star
        [user_type_image] => RisingStar141690.png
    )

  )         

我希望看起来像这样

 Array(
[0] => stdClass Object
    (
        [user_id] => 52
        [owner] => abc@gmail.com
        [sender] => Steve
        [alert_id] => 18
        [alert_community_id] => 1
        [alert_user_id] => 52
        [alert_text] => asdfasdf
        [alert_url] => asdfasdf-1
        [alert_note] => 
        [alert_source_proof] => asdfasdfasf
        [alert_proof_image] => 
        [is_time_critical] => 0
        [alert_reported_ip] => 80.255.4.52
        [alert_created_time] => 2015-03-15 15:25:20
        [direct_sending] => 0
        [ask_to_additional_reviwer] => 0
        [ask_additional_reviewer_at] => 
        [alert_reviewers] => 
        [alert_confirmed_by] => 56
        [confirmed_time] => 2015-03-15 19:26:35
        [alert_decliened_by] => 
        [decliened_time] => 
        [alert_status] => 1
        [community_id] => 1
        [hashtag] => #Nick
        [community_title] => Test community Nick #Nick asdfsa ds adsfa sd asdf asd asdf asf asdfa sdf
        [community_url] => test-community-nick-nick-asdfsa-ds-adsfa-sd-asdf-asd-asdf-as
        [user_type] => Apprentice
        [user_type_image] => Apprentice1416902127.png
    )

[1] => stdClass Object
    (
        [user_id] => 55
        [owner] => abc@gmail.com
        [sender] => Steve,chris,John
        [alert_id] => 17
        [alert_community_id] => 1
        [alert_user_id] => 55
        [alert_text] => asdfasdf
        [alert_url] => asdfasdf
        [alert_note] => 
        [alert_source_proof] => asdfasdfsadf
        [alert_proof_image] => 
        [is_time_critical] => 0
        [alert_reported_ip] => 80.255.4.52
        [alert_created_time] => 2015-03-15 15:20:34
        [direct_sending] => 0
        [ask_to_additional_reviwer] => 0
        [ask_additional_reviewer_at] => 
        [alert_reviewers] => 
        [alert_confirmed_by] => 54
        [confirmed_time] => 2015-03-15 19:21:19
        [alert_decliened_by] => 
        [decliened_time] => 
        [alert_status] => 1
        [community_id] => 1
        [hashtag] => #Nick
        [community_title] => Test community Nick #Nick asdfsa ds adsfa sd asdf asd asdf asf asdfa sdf
        [community_url] => test-community-nick-nick-asdfsa-ds-adsfa-sd-asdf-asd-asdf-as
        [user_type] => Rising star
        [user_type_image] => RisingStar141690.png
    )
  )

请帮帮我

1 个答案:

答案 0 :(得分:0)

最简单的方法是遍历数组并将unqiue对象保存到新数组中,例如:

$uniqueArray = array();
foreach($array as $object){
    if(!isset($uniqueArray[$object->user_id])){
        $uniqueArray[$object->user_id] = $object;
    }
}

评论后更新 您想要聚合一些数据,但结果数组仍然应该是每个user_id。因此,如果已经设置了具有user_id的对象,则将loop-part更改为concat:

$uniqueArray = array();
foreach($array as $object){
    if(!isset($uniqueArray[$object->user_id])){
        $uniqueArray[$object->user_id] = $object;
    }else{
        $uniqueArray[$object->user_id]->sender .= ',' . $object->sender;
    }
}

用示例测试:

<?php
$object1 =  new stdClass();
$object1->user_id = "52";
$object1->owner = "monadshare@gmail.com";
$object1->sender = "Steve";
$object1->alert_id = "18";
$object1->alert_community_id = "1";
$object1->alert_user_id = "52";
$object1->alert_text = "asdfasdf";
$object1->alert_url = "asdfasdf-1";
$object1->alert_note = "";
$object1->alert_source_proof = "asdfasdfasf";
$object1->alert_proof_image = "";
$object1->is_time_critical = "0";
$object1->alert_reported_ip = "80.255.4.52";
$object1->alert_created_time = "2015-03-15 15:25:20";
$object1->direct_sending = "0";
$object1->ask_to_additional_reviwer = "0";
$object1->ask_additional_reviewer_at = "";
$object1->alert_reviewers = "";
$object1->alert_confirmed_by = "56";
$object1->confirmed_time = "2015-03-15 19:26:35";
$object1->alert_decliened_by = "";
$object1->decliened_time = "";
$object1->alert_status = "1";
$object1->community_id = "1";
$object1->hashtag = "#Nick";
$object1->community_title = "Test community Nick #Nick asdfsa ds adsfa sd asdf asd asdf asf asdfa sdf";
$object1->community_url = "test-community-nick-nick-asdfsa-ds-adsfa-sd-asdf-asd-asdf-as";
$object1->user_type = "Apprentice";
$object1->user_type_image = "Apprentice1416902127.png";

$object2 =  new stdClass();
$object2->user_id = "55";
$object2->owner = "monadshare@gmail.com";
$object2->sender = "Steve";
$object2->alert_id = "17";
$object2->alert_community_id = "1";
$object2->alert_user_id = "55";
$object2->alert_text = "asdfasdf";
$object2->alert_url = "asdfasdf";
$object2->alert_note = "";
$object2->alert_source_proof = "asdfasdfsadf";
$object2->alert_proof_image = "";
$object2->is_time_critical = "0";
$object2->alert_reported_ip = "80.255.4.52";
$object2->alert_created_time = "2015-03-15 15:20:34";
$object2->direct_sending = "0";
$object2->ask_to_additional_reviwer = "0";
$object2->ask_additional_reviewer_at = "";
$object2->alert_reviewers = "";
$object2->alert_confirmed_by = "54";
$object2->confirmed_time = "2015-03-15 19:21:19";
$object2->alert_decliened_by = "";
$object2->decliened_time = "";
$object2->alert_status = "1";
$object2->community_id = "1";
$object2->hashtag = "#Nick";
$object2->community_title = "Test community Nick #Nick asdfsa ds adsfa sd asdf asd asdf asf asdfa sdf";
$object2->community_url = "test-community-nick-nick-asdfsa-ds-adsfa-sd-asdf-asd-asdf-as";
$object2->user_type = "Rising star";
$object2->user_type_image = "RisingStar141690.png";

$object3 =  new stdClass();
$object3->user_id = "55";
$object3->owner = "monadshare@gmail.com";
$object3->sender = "chris";
$object3->alert_id = "17";
$object3->alert_community_id = "1";
$object3->alert_user_id = "55";
$object3->alert_text = "asdfasdf";
$object3->alert_url = "asdfasdf";
$object3->alert_note = "";
$object3->alert_source_proof = "asdfasdfsadf";
$object3->alert_proof_image = "";
$object3->is_time_critical = "0";
$object3->alert_reported_ip = "80.255.4.52";
$object3->alert_created_time = "2015-03-15 15:20:34";
$object3->direct_sending = "0";
$object3->ask_to_additional_reviwer = "0";
$object3->ask_additional_reviewer_at = "";
$object3->alert_reviewers = "";
$object3->alert_confirmed_by = "54";
$object3->confirmed_time = "2015-03-15 19:21:19";
$object3->alert_decliened_by = "";
$object3->decliened_time = "";
$object3->alert_status = "1";
$object3->community_id = "1";
$object3->hashtag = "#Nick";
$object3->community_title = "Test community Nick #Nick asdfsa ds adsfa sd asdf asd asdf asf asdfa sdf";
$object3->community_url = "test-community-nick-nick-asdfsa-ds-adsfa-sd-asdf-asd-asdf-as";
$object3->user_type = "Rising star";
$object3->user_type_image = "RisingStar141690.png";

$object4 =  new stdClass();
$object4->user_id = "55";
$object4->owner = "monadshare@gmail.com";
$object4->sender = "John";
$object4->alert_id = "16";
$object4->alert_community_id = "1";
$object4->alert_user_id = "55";
$object4->alert_text = "asdf";
$object4->alert_url = "asdf-1";
$object4->alert_note = "";
$object4->alert_source_proof = "asfd";
$object4->alert_proof_image = "";
$object4->is_time_critical = "0";
$object4->alert_reported_ip = "80.255.4.52";
$object4->alert_created_time = "2015-03-15 15:08:48";
$object4->direct_sending = "0";
$object4->ask_to_additional_reviwer = "0";
$object4->ask_additional_reviewer_at = "";
$object4->alert_reviewers = "";
$object4->alert_confirmed_by = "56";
$object4->confirmed_time = "2015-03-15 19:09:16";
$object4->alert_decliened_by = "";
$object4->decliened_time = "";
$object4->alert_status = "1";
$object4->community_id = "1";
$object4->hashtag = "#Nick";
$object4->community_title = "Test community Nick #Nick asdfsa ds adsfa sd asdf asd asdf asf asdfa sdf";
$object4->community_url = "test-community-nick-nick-asdfsa-ds-adsfa-sd-asdf-asd-asdf-as";
$object4->user_type = "Rising star";
$object4->user_type_image = "RisingStar141690.png ";

$array = array($object1, $object2, $object3, $object4);

echo "<h1>Original array:</h1><pre>";
print_r($array);
echo "</pre>";

$uniqueArray = array();
foreach($array as $object){
    if(!isset($uniqueArray[$object->user_id])){
        $uniqueArray[$object->user_id] = $object;
    }else{
        $uniqueArray[$object->user_id]->sender .= ',' . $object->sender;
    }
}   

echo "<h1>Combined array</h1><pre>";
print_r($uniqueArray);
echo "</pre>";

?>