douzi2333
2017-07-19 23:38
浏览 106
已采纳

在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.

    已采纳该答案
    打赏 评论
  • dongyi8416 2017-07-20 01:29

    It seems that what is returned from the function api calls are json string, not php array or objects. Use json_decode() to convert it into an Object.

    There is nothing called 'EmployeeId' in this json. its a list of ID in the response string.

    {"readyState":4,"responseText":"string(2) \"32\"
    string(1) \"9\"
    string(2) \"67\"
    string(3) \"180\"
    ","status":200,"statusText":"OK"}
    

    You will have to

    $jsonarray = json_decode($_GET['CaseStaffs'],true);
    $listofIDs = $jsonarray['responseText']; // to get the list of IDs, as string
    

    But the string is going to look like:

    string(2) "32" string(1) "9" string(2) "67" string(3) "180"

    So you will have to figure out how to extract the IDs from the string

    Play around with this: https://tehplayground.com/murZXZSWQdCPBZHj

    打赏 评论

相关推荐 更多相似问题