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

时间:2017-07-19 23:38:37

标签: php mysql

嗨,所有人都迟到了,我认为疲倦会增加这一点,但我不能让这个PHP工作

我有一个用户类,并且在一个函数中通过id查找用户,然后将该人创建为对象。我正在尝试在foreach循环中创建此对象,以便从一系列ID中获取所有用户,以便我可以提取每个用户的电子邮件

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

$_GET['CaseStaffs']是我从api电话中获得的json工作人员ID数组

每个循环它接缝给我创建对象但是如果我尝试从对象访问任何值我得到

  

尝试获取非对象的属性

这是$return['staffEmail'][] = $user->email;

$admin['EmployeeId']

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

$user

的var转储
  api_test.php:127 {"readyState":4,"responseText":"object(Users)#7 (15) {\n  [\"id\"]=>\n  string(1) \"5\"\n  [\"email\"]=>\n  string(26) \"r.dymond@wilsonfield.co.uk\"\n  [\"pwd\"]=>\n  string(60) \"$2y$10$ZDNjMjU5YzkzMDQ5NzVjYehhZu1P300eGVpLS.prOwxTPeIiN8vQO\"\n  [\"salutation\"]=>\n  string(3) \"Mr.\"\n  [\"first\"]=>\n  string(6) \"Robert\"\n  [\"last\"]=>\n  string(6) \"Dymond\"\n  [\"status\"]=>\n  string(1) \"1\"\n  [\"account\"]=>\n  string(2) \"10\"\n  [\"optin\"]=>\n  string(1) \"0\"\n  [\"rep\"]=>\n  string(0) \"\"\n  [\"company_name\"]=>\n  string(0) \"\"\n  [\"reg\"]=>\n  string(19) \"2017-04-05 00:00:00\"\n  [\"token\"]=>\n  string(44) \"YThkNjBjYTdmNmE5YzFjZTY4MGNkMmE5NjVkY2I0MWM=\"\n  [\"assigned\"]=>\n  NULL\n  [\"date\"]=>\n  NULL\n}\nobject(Users)#8 (15) {\n  [\"id\"]=>\n  string(1) \"7\"\n  [\"email\"]=>\n  string(25) \"f.grant@wilsonfield.co.uk\"\n  [\"pwd\"]=>\n  string(60) \"$2y$10$ZDNjMjU5YzkzMDQ5NzVjYehhZu1P300eGVpLS.prOwxTPeIiN8vQO\"\n  [\"salutation\"]=>\n  string(0) \"\"\n  [\"first\"]=>\n  string(5) \"Fiona\"\n  [\"last\"]=>\n  string(5) \"Grant\"\n  [\"status\"]=>\n  string(1) \"1\"\n  [\"account\"]=>\n  string(2) \"10\"\n  [\"optin\"]=>\n  string(1) \"0\"\n  [\"rep\"]=>\n  string(0) \"\"\n  [\"company_name\"]=>\n  string(0) \"\"\n  [\"reg\"]=>\n  string(19) \"2017-04-05 00:00:00\"\n  [\"token\"]=>\n  string(44) \"Mjk5MmMwMzYyNWQ5OGQ5YTZhODQ1NjRjYTA2N2FkNDc=\"\n  [\"assigned\"]=>\n  NULL\n  [\"date\"]=>\n  NULL\n}\nbool(false)\n<br />\n<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 />\nobject(Users)#7 (15) {\n  [\"id\"]=>\n  string(2) \"18\"\n  [\"email\"]=>\n  string(27) \"J.Dobbins@wilsonfield.co.uk\"\n  [\"pwd\"]=>\n  string(60) \"$2y$10$NTFmNDY2NDBiMDk1MTcyNuLBr325ypXGFIoFxpeCPPhPFpv7lqjXq\"\n  [\"salutation\"]=>\n  string(3) \"Mr.\"\n  [\"first\"]=>\n  string(6) \"Joseph\"\n  [\"last\"]=>\n  string(7) \"Dobbins\"\n  [\"status\"]=>\n  string(1) \"1\"\n  [\"account\"]=>\n  string(2) \"30\"\n  [\"optin\"]=>\n  string(1) \"0\"\n  [\"rep\"]=>\n  string(0) \"\"\n  [\"company_name\"]=>\n  string(0) \"\"\n  [\"reg\"]=>\n  string(19) \"2017-04-06 11:57:52\"\n  [\"token\"]=>\n  string(44) \"M2Y0MTVmOGRkZTRkNDIyNDg5MWI4MmRlMzEyNzk5NzM=\"\n  [\"assigned\"]=>\n  NULL\n  [\"date\"]=>\n  NULL\n}\n","status":200,"statusText":"OK"}
api_test.php:127 {"readyState":4,"responseText":"object(Users)#7 (15) {\n  [\"id\"]=>\n  string(1) \"8\"\n  [\"email\"]=>\n  string(27) \"g.roberts@wilsonfield.co.uk\"\n  [\"pwd\"]=>\n  string(60) \"$2y$10$ZDNjMjU5YzkzMDQ5NzVjYehhZu1P300eGVpLS.prOwxTPeIiN8vQO\"\n  [\"salutation\"]=>\n  string(0) \"\"\n  [\"first\"]=>\n  string(5) \"Gemma\"\n  [\"last\"]=>\n  string(7) \"Roberts\"\n  [\"status\"]=>\n  string(1) \"1\"\n  [\"account\"]=>\n  string(2) \"10\"\n  [\"optin\"]=>\n  string(1) \"0\"\n  [\"rep\"]=>\n  string(0) \"\"\n  [\"company_name\"]=>\n  string(0) \"\"\n  [\"reg\"]=>\n  string(19) \"2017-04-05 00:00:00\"\n  [\"token\"]=>\n  string(44) \"OTI1Njk0MWM0YTc1MDE1ZTlmNjg1NWUzOTkxZDk0Zjc=\"\n  [\"assigned\"]=>\n  NULL\n  [\"date\"]=>\n  NULL\n}\nobject(Users)#8 (15) {\n  [\"id\"]=>\n  string(2) \"10\"\n  [\"email\"]=>\n  string(25) \"e.bower@wilsonfield.co.uk\"\n  [\"pwd\"]=>\n  string(60) \"$2y$10$ZDNjMjU5YzkzMDQ5NzVjYehhZu1P300eGVpLS.prOwxTPeIiN8vQO\"\n  [\"salutation\"]=>\n  string(0) \"\"\n  [\"first\"]=>\n  string(4) \"Emma\"\n  [\"last\"]=>\n  string(5) \"Bower\"\n  [\"status\"]=>\n  string(1) \"1\"\n  [\"account\"]=>\n  string(2) \"10\"\n  [\"optin\"]=>\n  string(1) \"0\"\n  [\"rep\"]=>\n  string(0) \"\"\n  [\"company_name\"]=>\n  string(0) \"\"\n  [\"reg\"]=>\n  string(19) \"2017-04-04 00:00:00\"\n  [\"token\"]=>\n  string(44) \"YzA1NGQwOWIzM2VmM2E2NWJkMmE0ZWMxYzQ5ZDdlYTM=\"\n  [\"assigned\"]=>\n  NULL\n  [\"date\"]=>\n  NULL\n}\nbool(false)\n<br />\n<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 />\nobject(Users)#7 (15) {\n  [\"id\"]=>\n  string(3) \"319\"\n  [\"email\"]=>\n  string(26) \"d.massey@wilsonfield.co.uk\"\n  [\"pwd\"]=>\n  string(60) \"$2y$10$Y2QyODdiMTc0ODA0ZDdmM.rMoco6Oy5JyPsOHudcDljXnsjvgUnWy\"\n  [\"salutation\"]=>\n  string(3) \"Mr.\"\n  [\"first\"]=>\n  string(6) \"Daniel\"\n  [\"last\"]=>\n  string(6) \"Massey\"\n  [\"status\"]=>\n  string(1) \"1\"\n  [\"account\"]=>\n  string(2) \"30\"\n  [\"optin\"]=>\n  string(1) \"0\"\n  [\"rep\"]=>\n  string(0) \"\"\n  [\"company_name\"]=>\n  string(0) \"\"\n  [\"reg\"]=>\n  string(19) \"2017-06-05 16:04:58\"\n  [\"token\"]=>\n  string(44) \"YTJmZDdhNDIzZjcyODA5NjFkMzBhMDhiMzlmMTg5MDA=\"\n  [\"assigned\"]=>\n  NULL\n  [\"date\"]=>\n  NULL\n}\n","status":200,"statusText":"OK"}

2 个答案:

答案 0 :(得分:0)

似乎从函数api调用返回的是json字符串,而不是php数组或对象。使用json_decode()将其转换为Object。

没有任何名为&#39; EmployeeId&#39;在这个json。它是响应字符串中的ID列表。

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

你必须

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

但字符串看起来像是:

string(2)&#34; 32&#34; 字符串(1)&#34; 9&#34; 字符串(2)&#34; 67&#34; string(3)&#34; 180&#34;

所以你必须弄清楚如何从字符串中提取ID

玩这个:https://tehplayground.com/murZXZSWQdCPBZHj

答案 1 :(得分:0)

$admin['EmployeeId']看起来很可疑,因为$admin$_GET['CaseStaffs']中的每个值。

看到这样的网址已经非常罕见了:

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

要看到这个更多(我不知道这种语法是否有效):

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

使用GET请求中的第一个URL,然后在您的代码中使用以下行:

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

另外,作为旁注,您应该一次检索所有用户,或者至少批量检索所有用户。一个接一个地做这件事很容易成为瓶颈。