简体   繁体   English

如何从php中返回的对象中检索值?

[英]How to retrieve value from returned object in php?

How to retrieve value from a complicated object structure in php?如何从php中复杂的对象结构中检索值? I know using '->' operator we can access the value but I am very confused in the object I am returned with.我知道使用 '->' 运算符我们可以访问该值,但我对返回的对象感到非常困惑。 From the object returned, I want to fetch the character value.从返回的对象中,我想获取字符值。 How do i do that?我怎么做? I am using Neo4jPHP and trying to execute a cypher query "MATCH (n) RETURN distinct keys(n)" to return all distinct property keys.我正在使用Neo4jPHP并尝试执行密码查询“MATCH (n) RETURN distinct keys(n)”以返回所有不同的属性键。 After doing a var_dump of the row object, the partial output is shown below.对行对象进行 var_dump 后,部分输出如下所示。请参见有关对象结构的图像

Edit :- My edited code after following Mikkel's advice:-编辑:- 在遵循 Mikkel 的建议后我编辑的代码:-

$keyquery="MATCH (n) RETURN distinct keys(n)"; 
$querykey=new Everyman\Neo4j\Cypher\Query($client, $keyquery);
$resultkey = $querykey->getResultSet();
foreach ($resultkey as $row) 
{
for($i=0;$i<count($row[0]);$i++)
{
echo $row[0][$i]; // returns all the property keys from the Row object
}
}

You can't access the object property directly as it was declared as protected (only accessible from within the class or an inheriting class).您不能直接访问对象属性,因为它被声明为受保护的(只能从类或继承类中访问)。

However, in such a case, the developer has usually added an object method or overloading function that allows you to access the information you're looking for.但是,在这种情况下,开发人员通常会添加一个对象方法或重载函数,允许您访问您要查找的信息。 Taking a peek at the source , it looks like you should be able to access the data you're looking for using either:查看源代码,看起来您应该能够使用以下任一方式访问您要查找的数据:

// this works because the class implements Iterator
foreach ($myobject as $row) {
    echo $row['keys(n)']; // outputs "character"
}

or:或者:

// this works because the class implements ArrayAccess
// don't ask me why they put keys and values in different arrays ('columns' and 'raw')
echo $myobject[0]['keys(n)']; // outputs "character"

The value you are looking for is protected and not accessible,您正在寻找的价值受到保护且无法访问,

  1. try find object class and add function to retrieve the value.尝试查找对象类并添加函数来检索值。
  2. use regular expression to extract the portion, which is not recommended: /\\'character\\'(length\\=(.*?))/使用正则表达式提取部分,不推荐使用: /\\'character\\'(length\\=(.*?))/

如果您查看Row类,您会发现您可以访问它,将对象视为数组。

$character = $myRow[0];

Looking at the object you dumped here , you can see that the object is implementing \\Iterator, \\Countable, \\ArrayAccess, which means you can basically treat it like an array.查看您在此处转储的对象,您可以看到该对象正在实现 \\Iterator、\\Countable、\\ArrayAccess,这意味着您基本上可以将其视为数组。 The underlying data source is the protected $raw.底层数据源是受保护的 $raw。

$queryResult = ...;

foreach ($queryResult as $row) {
   echo $row['character'] . PHP_EOL;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM