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条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行