Jump to content
TiagoSilva

How to filter an array?

Recommended Posts

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] => ) )

Share this post


Link to post
Share on other sites

"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.com
that contain "ID" and "user_email" properties.

 

foreach ($variable as $user) {
	echo "ID: {$user->data->ID}, Email: {$user->data->user_email}";
}

Share this post


Link to post
Share on other sites

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 by TiagoSilva

Share this post


Link to post
Share on other sites

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.

  • Like 1

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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,

Share this post


Link to post
Share on other sites

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 by TiagoSilva

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.