doubi4814 2017-09-26 07:39
浏览 19
已采纳

从数组中为每个循环构建简单的PHP

I have an array called $user_ids that prints as:

Array ( [0] => stdClass Object ( [user_id] => 1 ) [1] => stdClass Object ( [user_id] => 2 ) )

I want to perform send_msg for every user_id in the array. In the example above, I want to achieve the equivalent of this:

send_msg( 1, $body_input, $subject_input);  
send_msg( 2, $body_input, $subject_input);  

This is what I have tried but it doesn't work.

foreach ($user_ids as $user_N){
send_msg( $user_N, $body_input, $subject_input);    
}
  • 写回答

4条回答 默认 最新

  • doutao4480 2017-09-26 07:46
    关注

    In PHP >= 7.0.0 you can extract all of the user_ids from the objects with array_column:

    foreach(array_column($user_ids, 'user_id') as $user_N) {
        send_msg($user_N, $body_input, $subject_input);    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部