douzi2333 2017-07-19 15:38
浏览 117
已采纳

在foreach循环中实例化静态对象

Hi all its late and I think tiredness is adding to this but I cant get this php to work

I have a Users Class and in a function to find users by an id and then create the person as an object. I'm trying to create this object in a foreach loop to get all users from an array of ids so i can then extract each users email

foreach ($_GET['CaseStaffs'] as $admin ) {
    $user = Users::find_by_ips_id( $admin['EmployeeId'] );
    $return['staffEmail'][] = $user->email;
}
echo json_encode($return);

$_GET['CaseStaffs'] is a json array of staff member ids i'm getting from an api call

each loop it seams to me creating the object but if i try and access any value from the object i get

Trying to get property of non-object

this is in the line $return['staffEmail'][] = $user->email;

var dump of $admin['EmployeeId']

  {"readyState":4,"responseText":"string(2) \"32\"
string(1) \"9\"
string(2) \"67\"
string(3) \"180\"
","status":200,"statusText":"OK"}
  api_test.php:127 {"readyState":4,"responseText":"string(2) \"10\"
string(1) \"7\"
string(2) \"92\"
string(3) \"148\"
","status":200,"statusText":"OK"}

var dump of $user

  api_test.php:127 {"readyState":4,"responseText":"object(Users)#7 (15) {
  [\"id\"]=>
  string(1) \"5\"
  [\"email\"]=>
  string(26) \"r.dymond@wilsonfield.co.uk\"
  [\"pwd\"]=>
  string(60) \"$2y$10$ZDNjMjU5YzkzMDQ5NzVjYehhZu1P300eGVpLS.prOwxTPeIiN8vQO\"
  [\"salutation\"]=>
  string(3) \"Mr.\"
  [\"first\"]=>
  string(6) \"Robert\"
  [\"last\"]=>
  string(6) \"Dymond\"
  [\"status\"]=>
  string(1) \"1\"
  [\"account\"]=>
  string(2) \"10\"
  [\"optin\"]=>
  string(1) \"0\"
  [\"rep\"]=>
  string(0) \"\"
  [\"company_name\"]=>
  string(0) \"\"
  [\"reg\"]=>
  string(19) \"2017-04-05 00:00:00\"
  [\"token\"]=>
  string(44) \"YThkNjBjYTdmNmE5YzFjZTY4MGNkMmE5NjVkY2I0MWM=\"
  [\"assigned\"]=>
  NULL
  [\"date\"]=>
  NULL
}
object(Users)#8 (15) {
  [\"id\"]=>
  string(1) \"7\"
  [\"email\"]=>
  string(25) \"f.grant@wilsonfield.co.uk\"
  [\"pwd\"]=>
  string(60) \"$2y$10$ZDNjMjU5YzkzMDQ5NzVjYehhZu1P300eGVpLS.prOwxTPeIiN8vQO\"
  [\"salutation\"]=>
  string(0) \"\"
  [\"first\"]=>
  string(5) \"Fiona\"
  [\"last\"]=>
  string(5) \"Grant\"
  [\"status\"]=>
  string(1) \"1\"
  [\"account\"]=>
  string(2) \"10\"
  [\"optin\"]=>
  string(1) \"0\"
  [\"rep\"]=>
  string(0) \"\"
  [\"company_name\"]=>
  string(0) \"\"
  [\"reg\"]=>
  string(19) \"2017-04-05 00:00:00\"
  [\"token\"]=>
  string(44) \"Mjk5MmMwMzYyNWQ5OGQ5YTZhODQ1NjRjYTA2N2FkNDc=\"
  [\"assigned\"]=>
  NULL
  [\"date\"]=>
  NULL
}
bool(false)
<br />
<b>Notice</b>:  Trying to get property of non-object in <b>C:\\Users\\jfiel\\Desktop\\document.center.new\\httpdocs\\ajax\\process\\cases.php</b> on line <b>90</b><br />
object(Users)#7 (15) {
  [\"id\"]=>
  string(2) \"18\"
  [\"email\"]=>
  string(27) \"J.Dobbins@wilsonfield.co.uk\"
  [\"pwd\"]=>
  string(60) \"$2y$10$NTFmNDY2NDBiMDk1MTcyNuLBr325ypXGFIoFxpeCPPhPFpv7lqjXq\"
  [\"salutation\"]=>
  string(3) \"Mr.\"
  [\"first\"]=>
  string(6) \"Joseph\"
  [\"last\"]=>
  string(7) \"Dobbins\"
  [\"status\"]=>
  string(1) \"1\"
  [\"account\"]=>
  string(2) \"30\"
  [\"optin\"]=>
  string(1) \"0\"
  [\"rep\"]=>
  string(0) \"\"
  [\"company_name\"]=>
  string(0) \"\"
  [\"reg\"]=>
  string(19) \"2017-04-06 11:57:52\"
  [\"token\"]=>
  string(44) \"M2Y0MTVmOGRkZTRkNDIyNDg5MWI4MmRlMzEyNzk5NzM=\"
  [\"assigned\"]=>
  NULL
  [\"date\"]=>
  NULL
}
","status":200,"statusText":"OK"}
api_test.php:127 {"readyState":4,"responseText":"object(Users)#7 (15) {
  [\"id\"]=>
  string(1) \"8\"
  [\"email\"]=>
  string(27) \"g.roberts@wilsonfield.co.uk\"
  [\"pwd\"]=>
  string(60) \"$2y$10$ZDNjMjU5YzkzMDQ5NzVjYehhZu1P300eGVpLS.prOwxTPeIiN8vQO\"
  [\"salutation\"]=>
  string(0) \"\"
  [\"first\"]=>
  string(5) \"Gemma\"
  [\"last\"]=>
  string(7) \"Roberts\"
  [\"status\"]=>
  string(1) \"1\"
  [\"account\"]=>
  string(2) \"10\"
  [\"optin\"]=>
  string(1) \"0\"
  [\"rep\"]=>
  string(0) \"\"
  [\"company_name\"]=>
  string(0) \"\"
  [\"reg\"]=>
  string(19) \"2017-04-05 00:00:00\"
  [\"token\"]=>
  string(44) \"OTI1Njk0MWM0YTc1MDE1ZTlmNjg1NWUzOTkxZDk0Zjc=\"
  [\"assigned\"]=>
  NULL
  [\"date\"]=>
  NULL
}
object(Users)#8 (15) {
  [\"id\"]=>
  string(2) \"10\"
  [\"email\"]=>
  string(25) \"e.bower@wilsonfield.co.uk\"
  [\"pwd\"]=>
  string(60) \"$2y$10$ZDNjMjU5YzkzMDQ5NzVjYehhZu1P300eGVpLS.prOwxTPeIiN8vQO\"
  [\"salutation\"]=>
  string(0) \"\"
  [\"first\"]=>
  string(4) \"Emma\"
  [\"last\"]=>
  string(5) \"Bower\"
  [\"status\"]=>
  string(1) \"1\"
  [\"account\"]=>
  string(2) \"10\"
  [\"optin\"]=>
  string(1) \"0\"
  [\"rep\"]=>
  string(0) \"\"
  [\"company_name\"]=>
  string(0) \"\"
  [\"reg\"]=>
  string(19) \"2017-04-04 00:00:00\"
  [\"token\"]=>
  string(44) \"YzA1NGQwOWIzM2VmM2E2NWJkMmE0ZWMxYzQ5ZDdlYTM=\"
  [\"assigned\"]=>
  NULL
  [\"date\"]=>
  NULL
}
bool(false)
<br />
<b>Notice</b>:  Trying to get property of non-object in <b>C:\\Users\\jfiel\\Desktop\\document.center.new\\httpdocs\\ajax\\process\\cases.php</b> on line <b>90</b><br />
object(Users)#7 (15) {
  [\"id\"]=>
  string(3) \"319\"
  [\"email\"]=>
  string(26) \"d.massey@wilsonfield.co.uk\"
  [\"pwd\"]=>
  string(60) \"$2y$10$Y2QyODdiMTc0ODA0ZDdmM.rMoco6Oy5JyPsOHudcDljXnsjvgUnWy\"
  [\"salutation\"]=>
  string(3) \"Mr.\"
  [\"first\"]=>
  string(6) \"Daniel\"
  [\"last\"]=>
  string(6) \"Massey\"
  [\"status\"]=>
  string(1) \"1\"
  [\"account\"]=>
  string(2) \"30\"
  [\"optin\"]=>
  string(1) \"0\"
  [\"rep\"]=>
  string(0) \"\"
  [\"company_name\"]=>
  string(0) \"\"
  [\"reg\"]=>
  string(19) \"2017-06-05 16:04:58\"
  [\"token\"]=>
  string(44) \"YTJmZDdhNDIzZjcyODA5NjFkMzBhMDhiMzlmMTg5MDA=\"
  [\"assigned\"]=>
  NULL
  [\"date\"]=>
  NULL
}
","status":200,"statusText":"OK"}

展开全部

  • 写回答

2条回答 默认 最新

  • duanjianao0592 2017-07-20 06:03
    关注

    $admin['EmployeeId'] looks suspicious to me, because $admin is each of the values in $_GET['CaseStaffs'].

    It is already quite uncommon to see URLs like these:

    http://example.com/file.php?CaseStaffs[]=1&CaseStaffs[]=2&CaseStaffs[]=3
    

    Much more to see this (I don't know if this syntax is valid):

    http://example.com/file.php?CaseStaffs[]['EmployeeId']=1&CaseStaffs[]['EmployeeId']=2&CaseStaffs[]['EmployeeId']=3
    

    Use the first URL in the GET request and then, in your code, use this line:

    $user = Users::find_by_ips_id( $admin );
    

    Also, as a side note, you should retrieve all your users at once, or at least in batches. Doing it one by one can easily become a bottleneck.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 指定IP电脑的访问设置
  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
手机看
程序员都在用的中文IT技术交流社区

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

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

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

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

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

客服 返回
顶部