TiagoSilva Posted July 24, 2016 Share Posted July 24, 2016 Hey guys,Ive spent weeks trying to filter this array. Can someone help me? I want to filter and display only user ID´s or only emails. How do I do that?Array ( [2] => WP_User Object ( [data] => stdClass Object ( [iD] => 3 [user_login] => test [user_pass] => $P$FSBSFByN.kDA20C2B1HLYtossdD1 [user_nicename] => test [user_email] => noreply1@test.com [user_url] => [user_registered] => 2016-06-28 06:41:10 [user_activation_key] => [user_status] => 0 [display_name] => Albert Einstein ) [iD] => 3 [caps] => Array ( [subscriber] => 1 ) [cap_key] => wp_capabilities [roles] => Array ( [0] => subscriber ) [allcaps] => Array ( [read] => 1 [level_0] => 1 [subscriber] => 1 ) [filter] => ) [3] => WP_User Object ( [data] => stdClass Object ( [iD] => 1 [user_login] => Almert [user_pass] => $P$BXkFcAfewww771GjlUxmm.696EB0 [user_nicename] => Goof [user_email] => tiadgw3@hotmail.com [user_url] => [user_registered] => 2016-06-25 09:54:12 [user_activation_key] => [user_status] => 0 [display_name] => tiadgw3 ) [iD] => 1 [caps] => Array ( [administrator] => 1 ) [cap_key] => wp_capabilities [roles] => Array ( [0] => administrator ) [allcaps] => Array ( [switch_themes] => 1 [edit_themes] => 1 [activate_plugins] => 1 [edit_plugins] => 1 [edit_users] => 1 [edit_files] => 1 [manage_options] => 1 [moderate_comments] => 1 [manage_categories] => 1 [manage_links] => 1 [upload_files] => 1 [import] => 1 [unfiltered_html] => 1 [edit_posts] => 1 [edit_others_posts] => 1 [edit_published_posts] => 1 [publish_posts] => 1 [edit_pages] => 1 [read] => 1 [level_10] => 1 [level_9] => 1 [level_8] => 1 [level_7] => 1 [level_6] => 1 [level_5] => 1 [level_4] => 1 [level_3] => 1 [level_2] => 1 [level_1] => 1 [level_0] => 1 [edit_others_pages] => 1 [edit_published_pages] => 1 [publish_pages] => 1 [delete_pages] => 1 [delete_others_pages] => 1 [delete_published_pages] => 1 [delete_posts] => 1 [delete_others_posts] => 1 [delete_published_posts] => 1 [delete_private_posts] => 1 [edit_private_posts] => 1 [read_private_posts] => 1 [delete_private_pages] => 1 [edit_private_pages] => 1 [read_private_pages] => 1 [delete_users] => 1 [create_users] => 1 [unfiltered_upload] => 1 [edit_dashboard] => 1 [update_plugins] => 1 [delete_plugins] => 1 [install_plugins] => 1 [update_themes] => 1 [install_themes] => 1 [update_core] => 1 [list_users] => 1 [remove_users] => 1 [promote_users] => 1 [edit_theme_options] => 1 [delete_themes] => 1 [export] => 1 [edit_newsletters] => 1 [publish_newsletters] => 1 [delete_newsletters] => 1 [edit_others_newsletters] => 1 [delete_others_newsletters] => 1 [duplicate_newsletters] => 1 [duplicate_others_newsletters] => 1 [mymail_edit_autoresponders] => 1 [mymail_edit_others_autoresponders] => 1 [mymail_change_template] => 1 [mymail_save_template] => 1 [mymail_see_codeview] => 1 [mymail_change_plaintext] => 1 [mymail_edit_subscribers] => 1 [mymail_add_subscribers] => 1 [mymail_delete_subscribers] => 1 [mymail_edit_forms] => 1 [mymail_add_forms] => 1 [mymail_delete_forms] => 1 [mymail_manage_subscribers] => 1 [mymail_import_subscribers] => 1 [mymail_import_wordpress_users] => 1 [mymail_export_subscribers] => 1 [mymail_bulk_delete_subscribers] => 1 [mymail_add_lists] => 1 [mymail_edit_lists] => 1 [mymail_delete_lists] => 1 [mymail_manage_templates] => 1 [mymail_edit_templates] => 1 [mymail_delete_templates] => 1 [mymail_upload_templates] => 1 [mymail_update_templates] => 1 [mymail_dashboard_widget] => 1 [mymail_manage_capabilities] => 1 [manage_sws] => 1 [rcp_view_members] => 1 [rcp_manage_members] => 1 [rcp_view_levels] => 1 [rcp_manage_levels] => 1 [rcp_view_discounts] => 1 [rcp_manage_discounts] => 1 [rcp_view_payments] => 1 [rcp_manage_payments] => 1 [rcp_manage_settings] => 1 [rcp_export_data] => 1 [rcp_view_help] => 1 [modal_login_settings] => 1 [administrator] => 1 ) [filter] => ) ) Quote Link to comment https://forums.phpfreaks.com/topic/301576-how-to-filter-an-array/ Share on other sites More sharing options...
requinix Posted July 25, 2016 Share Posted July 25, 2016 "Weeks"? Really? Array (Whatever variable you dumped is an array, [2] => WP_User Object ( [3] => WP_User Object (containing WP_User objects, [data] => stdClass Object (with "data" properties that are plain stdClass objects, [ID] => 3 [user_email] => noreply1@test.comthat contain "ID" and "user_email" properties. foreach ($variable as $user) { echo "ID: {$user->data->ID}, Email: {$user->data->user_email}"; } Quote Link to comment https://forums.phpfreaks.com/topic/301576-how-to-filter-an-array/#findComment-1534964 Share on other sites More sharing options...
TiagoSilva Posted July 25, 2016 Author Share Posted July 25, 2016 (edited) Hello, Thanks for the help. So it starts like this: <?php$userlist = get_users_who_favorited_post($post_id = null, $site_id = null); <--- gives the array above what should I type here? print_r($userlist)?> And I want a formula to get only ID´s .....and another formula to obtain only emails... Thanks Edited July 25, 2016 by TiagoSilva Quote Link to comment https://forums.phpfreaks.com/topic/301576-how-to-filter-an-array/#findComment-1534982 Share on other sites More sharing options...
requinix Posted July 25, 2016 Share Posted July 25, 2016 I showed you code. You need to make the mental leap to understand what it is and what it does. It's only three lines. More of a step than a leap. 1 Quote Link to comment https://forums.phpfreaks.com/topic/301576-how-to-filter-an-array/#findComment-1534988 Share on other sites More sharing options...
TiagoSilva Posted July 25, 2016 Author Share Posted July 25, 2016 Hello, You did it! =) <?php$userlist = get_users_who_favorited_post($post_id = null, $site_id = null);foreach ($userlist as $user) {echo "{$user->data->ID}";}?> Another question. I want to echo only the first ID number .....or only que second ID number ....how can I do that? Quote Link to comment https://forums.phpfreaks.com/topic/301576-how-to-filter-an-array/#findComment-1535004 Share on other sites More sharing options...
TiagoSilva Posted July 26, 2016 Author Share Posted July 26, 2016 Hello,I tried with substr ...but no success until now..<?php$userlist = get_users_who_favorited_post($post_id = null, $site_id = null);foreach ($userlist as $user) {$int="{$user->data->ID}"; //returns 1234 (Example)}echo substr($int, 1, 1);?>its not working. is there an alternative to substr?? any sugestions?Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/301576-how-to-filter-an-array/#findComment-1535070 Share on other sites More sharing options...
cyberRobot Posted July 26, 2016 Share Posted July 26, 2016 To get the first element in an array, you could use array_shift(). More information can be found here: http://php.net/manual/en/function.array-shift.php With that, you probably don't need the foreach loop. But I'm not sure if that solves the second part of your question. Quote Link to comment https://forums.phpfreaks.com/topic/301576-how-to-filter-an-array/#findComment-1535079 Share on other sites More sharing options...
TiagoSilva Posted July 27, 2016 Author Share Posted July 27, 2016 (edited) Hey Guys,Can someone give a little help?I found that it returns 2 arrays. For example Array ( [0] => 3 ) Array ( [0] => 1 ) ....thats why the substr() is not working. Because if I tried to get the first element it would give me "31". And the second element would be "empty".<?php$userlist = get_users_who_favorited_post($post_id = null, $site_id = null);foreach ($userlist as $user) {$int= "{$user->data->ID}"; // 31 (not a sequence like I thought)$arr1 = str_split($int); print_r($arr1); //Array ( [0] => 3 ) Array ( [0] => 1 )}?>I want to obtain the first element "3" .....and the second element "1" ....separately ...... how can i do this?Thanks, Edited July 27, 2016 by TiagoSilva Quote Link to comment https://forums.phpfreaks.com/topic/301576-how-to-filter-an-array/#findComment-1535121 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.