douzi2333 2017-07-19 23: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 14: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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!