简体   繁体   中英

How to retrieve value from returned object in php?

How to retrieve value from a complicated object structure in 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. After doing a var_dump of the row object, the partial output is shown below.请参见有关对象结构的图像

Edit :- My edited code after following Mikkel's advice:-

$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\\=(.*?))/

如果您查看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. The underlying data source is the protected $raw.

$queryResult = ...;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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