如何正确访问数组对象(How to properly access to array objects)
我有一个来自webservice查询的结果,我想从中获取一些值。 它有效,但我有PHP通知问题,所以我可能做错了。
这是
$items
变量内容:stdClass Object ( [response] => stdClass Object ( [0] => stdClass Object ( [id] => 275 [corpid] => 16107 [name] => default [description] => [status] => ok [nbSteps] => 7 ) [defaultItem] => 275 ) [error] => [status] => success )
所以我尝试了类似的东西:
foreach ( $items->response AS $key => $item ) { if ( $item->name == 'default' ){ // Line 106 $Id = $item->id; } }
它工作,
$Id
等于275,但PHP返回一个通知:Notice: Trying to get property of non-object in /home/web/dev/webservice-form.php on line 106
任何帮助将不胜感激。
编辑:这是$ item变量的内容(取自foreach循环):
stdClass Object ( [id] => 275 [corpid] => 16107 [name] => default [description] => [status] => ok [nbSteps] => 7 ) 275
请注意,'275'是结果的一部分。
I've a result from a webservice's query and I would like to get some values from it. It works but I have PHP notice issues, so I'm probably doing something wrong.
This is the
$items
variable content :stdClass Object ( [response] => stdClass Object ( [0] => stdClass Object ( [id] => 275 [corpid] => 16107 [name] => default [description] => [status] => ok [nbSteps] => 7 ) [defaultItem] => 275 ) [error] => [status] => success )
So I tried something like :
foreach ( $items->response AS $key => $item ) { if ( $item->name == 'default' ){ // Line 106 $Id = $item->id; } }
It works,
$Id
is equal to 275 but PHP returns a notice :Notice: Trying to get property of non-object in /home/web/dev/webservice-form.php on line 106
Any help would be greatly appreciated.
EDIT : This is the content of the $item variable (taken from the foreach loop) :
stdClass Object ( [id] => 275 [corpid] => 16107 [name] => default [description] => [status] => ok [nbSteps] => 7 ) 275
Please note that the '275' is a part of the result.
原文:https://stackoverflow.com/questions/15089936
最满意答案
您只能从函数返回一次。 构建一个结果数组并返回:
$result = mysql_query("SELECT * FROM project ORDER BY projectid"); $values = array(); while($row = mysql_fetch_array($result)) { $values[] = array($row['projectid'], $row['clientname'], $row['salesperson'], $row['prospect']); } return $values;
You can only return once from a function. Build an array of results and return that:
$result = mysql_query("SELECT * FROM project ORDER BY projectid"); $values = array(); while($row = mysql_fetch_array($result)) { $values[] = array($row['projectid'], $row['clientname'], $row['salesperson'], $row['prospect']); } return $values;
相关问答
更多-
sqlite不返回从表中选择的值不在其他表中(sqlite not returning values selected from table not in other table)[2022-03-23]
我想在table2.id可以table2.id值? NOT IN ( ..., null, ...) 不幸的是(因为 - 所以论证 - 空值是未知值,并且我们不能保证未知值显然不是你正在寻找的值)。 一个糟糕的陷阱。 尝试 SELECT COUNT(id) FROM table1 WHERE id NOT IN (SELECT id FROM table2 where id is not null); 或者是一个EXISTS子句。 I suppose there can be nulls in tabl ... -
你可以使用`GROUP_CONCAT'来实现它: SELECT p.id, p.name, GROUP_CONCAT(num SEPARATOR ', ') FROM people as p INNER JOIN contacts as c on p.id = c.per_id GROUP BY p.id You can achieve it using `GROUP_CONCAT': SELECT p.id, p.name, GROUP_CONCAT(num SEPARATOR ', ') FRO ...
-
我并不完全明白你要做什么。 如果每列都是空字符串或空字符串,您是否需要指示? 如果是这样,您有一些错误: SELECT if(b2b_image1 is null or b2b_image1='' ,'1', '0') ,if(b2b_image2 is null or b2b_image2='', '1', '0') ,if(b2b_image3 is null or b2b_image3='', '1', '0') ,if(b2b_image4 is null o ...
-
mtcars[mtcars$cyl == 4, c(1,4)] 要么 with(mtcars, mtcars[cyl == 4, c(1,4)]) 要么 subset(mtcars, cyl == 4, select = c("hp", "mpg")) 请注意,输入c(1,4)等同于c("hp", "mpg")是另一位提到的评论者。 mtcars[mtcars$cyl == 4, c(1,4)] or with(mtcars, mtcars[cyl == 4, c(1,4)]) or subset ...
-
一些表值返回null?(Some table values returning null?)[2023-07-04]
表中有一些有趣的撇号值。 从字符串中删除[']并将其替换为[']后,该值不再为空。 There are some values in the table with interesting apostrophes.. After removing [ ’ ] from the string and replacing it with [ ' ] the value is no longer null. -
即使mysql表中有值,SQL查询也不返回值(SQL query is not returning values even if there are values in mysql Table)[2023-11-24]
因为您只需要在结果行中输入一列。 :) 你只能作为accountId k ... private function _loadColumnRights() { $db = Zend_Registry::get('db'); // Create the select query $select = $db->select() // THERE IS YOU ERROR :) ... -
您只能从函数返回一次。 构建一个结果数组并返回: $result = mysql_query("SELECT * FROM project ORDER BY projectid"); $values = array(); while($row = mysql_fetch_array($result)) { $values[] = array($row['projectid'], $row['clientname'], $row['salesperson'], $row['prospect']); ...
-
循环遍历整个表以检查sql server的值(looping throught the entire table to check for values sql server)[2023-11-17]
只需在你的第一个aql查询中使用它--voucher_id = @ voucher_id ALTER PROCEDURE [dbo].[spUpdateVoucherNo] ( @voucher_id as int, @voucher_separate_numbering as varchar(2), @voucher_method_numbering as varchar(2), @voucher_last_numb ... -
比较一个表中的重复行,并返回另一个表中的值(Comparing duplicate rows in one table, and returning values in another table)[2022-06-08]
就像是? SELECT * FROM table2 WHERE table2.id NOT IN (SELECT DISTINCT table1.id FROM table1 WHERE table1.checkin > sysdate OR table1.checkout > sysdate) AND table2.id IN (SELECT ... -
您的代码没问题,但我不相信您的查询结果。 尝试在maxIdNum上设置负值,以查看您是否从查询中获得结果。 int maxIdNum = -1; 您可以做的另一项测试是替换rsx.getInt("ID"); 通过rs.getString("ID"); 要知道结果是否为字符串,如果是int则会失败,这样可以帮助您找出问题所在。 您可以做的另一个是使用硬编码结果设置查询以了解是否存在其他问题: select 1 as ID from dual You code is ok, but I don't trus ...