Parsing PHP array which has an abject inside - php

I've got a response form the solr query in php. below is the response form apache solr in drupal6, i need to access the id field inside the Apache_Solr_Document, can some bosy help me with this.
i was able to print this using print_r($result);
Array ( [type] => Bookmarks [node] => Apache_Solr_Document Object ( [_documentBoost:protected] => [_fields:protected] => Array ( [id] => b17692e4ad53/node/274 )))
if i do print_r($result[node]); i am getting
Apache_Solr_Document Object ( [_documentBoost:protected] => [_fields:protected] => Array ( [id] => b17692e4ad53/node/274 ))
from here i can't figure out how to access the id.

Have you tried reading through IBM's article on Solr? The end of the article deals specifically with PHP.
Edit: After finding a source class to read through, it looks like you can do:
$result['node']->getField("id");
or using the magic __get:
$result['node']->id;

You can try using the {} brackets if the object name is not a valid object property name (like SimpleXML stuff).
$object->{'strang&$*#nmame'}->value;

Related

Not able to access attributes in xml

Am trying to access the attribute of the simple xml
SimpleXMLElement Object
(
[#attributes] => Array
(
[Index] => 21
)
[Data] => Hello world
)
i want to access Index attribute. I tried the following code but its not
working for me
$xml->attributes()->Index
There you go my friend:
$xml->attributes()['Index']
Sometimes I noticed I had to cast the result when it's a string. Like:
(string)$xml->attributes()['Index']

php get array values

I have this array called inputs:
Array ( [0] => InputValidation Object ( [array:InputValidation:private] => Array ( [Id] => demo1 [Required] => [MinLength] => 10 [MaxLength] => [RegexName] => [RegexMsg] => ) ) [1] => InputValidation Object ( [array:InputValidation:private] => Array ( [Id] => demo2 [Required] => [MinLength] => 20 [MaxLength] => [RegexName] => [RegexMsg] => ) ) )
I must get value Id, Required, MinLength, MaxLength, RegexName and RegexMsg. I have tried to foreach like this
foreach ($this->inputs as $input){
echo $input['Id'];
}
But it give me an error: Cannot use object of type InputValidation as array
How can I do? Thanks
inputs is an array in which the first element (index 0) is an object.
But in your loop, you try to echo an object property as if the object was an array.
Try:
foreach ($this->inputs as $input){
echo $input->Id;
}
Compared to JavaScript, objects in PHP cannot be accessed using the [] accessor, you have to use the -> accessor.
Yes I made a mistake.
Array inputs has 1st index with an object InputValidation.
This object also have a PRIVATE property named InputValidation (which is an associative array, where located the IdIindex you're looking for).
But this property is private. You won't be able to access it directly. You need a method on object InputValidation for that.
If that propery wasn't private but public, you woukd be able to access it as follow:
inputs [0]-> InputValidation-> InputValidation['Id'];
1st advice: avoid having an object which has a property with the same name than this object. That is confusing (I'd been trapped in it!)
2nd advice: read the doc about the objects you're using and PHP objects doc.
The advice from Comfreek looks like new feature in PHP or something that required well knowledge of it.

How to get data from array in object

I can't seem to get specific data from an array inside an object.
$this->fields->adres gets the address correctly, but i can't get a level deeper.
I've tried:
$this->fields->province
$this->fields->province->0
$this->fields->province[0]
And: (edit)
$this->fields["province"][0]
$this->fields['province'][0]
$this->data->fields['province'][0]
But it does not return anything while it should return "Flevoland".
First part of the object print_r($this, TRUE) below:
RSMembershipModelSubscribe Object
(
[_id] => 2
[_extras] => Array
(
)
[_data] => stdClass Object
(
[username] => testzz
[name] => testzz
[email] => xxxx#example.com
[fields] => Array
(
[province] => Array
(
[0] => Flevoland
)
[plaats] => tesdt
[adres] => test
You can also use type casting.
$fields = (array) $this->data->fields;
echo $fields['province'][0];
As you can see by your output, object members are likely to be private (if you follow conventions, anyway you must prepend an underscore while calling them), so you're calling them the wrong way;
This code works:
$this->_data->fields['province'][0];
You can see it in action here;
I created a similar object, and using
$membership = new RSMembershipModelSubscribe();
echo $membership->_data->fields['province'][0];
outputs "Flevoland" as expected.
As fields is already an array, try this:
$this->fields['province'][0]
This assuming the [_data] object is $this.
Fields and province are both arrays, you should be trying $this->fields["province"][0]
$this->_data->fields['province'][0]

how to get to a specific part of this nested array/object

This is driving me crazy, I am trying to get to a specific part of this object and it is driving me crazy, here is the object contents:
XMLHandler Object
(
[doc:XMLHandler:private] => SimpleXMLElement Object
(
[#attributes] => Array
(
[state] => Live
)
[newsListItem] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[href] => http://api.contentplus.co.uk/6cb5ea15-d6b1-4c40-9db7-cb2a3315080b/news/800773226/
)
[id] => 800773226
[publishDate] => 2011-10-24T10:04:49
[lastModifiedDate] => 2011-10-24T11:20:40
[headline] => Relationships matter on social media
)
)
)
[format] => html
)
I want to get the value of [id] I am trying to access it like this:
echo $niList->doc->newsListItem[0]->id;
but this is giving me nothing, I know I am close (well I hope I am) but I just cant quite get it right, could anyone help please.
Thanks all.
Your object dump says
doc:XMLHandler:private
which means doc is a private property of XMLHandler. As such, you can only access it from within that object via $this. But you are trying to access it from outside the object when you do
echo $niList->newsListItem[0]->id;
This wont work. Add a method to that XMLHandler object that does what you want to do with that newslistitem id. Also see the chapter on Visibility in the PHP Manual:
http://docs.php.net/manual/en/language.oop5.visibility.php

PHP stdClass Object arrays [_]=> syntax

I am trying to access the 1st part of the following Object.
I'm unable to figure out what is the [_] => syntax?
If I try to print the 1st past of the Object using Body->[_] it errors out.
[Body] => stdClass Object
(
[_] => http://xyz.com
http://MPQ.com
[BodyType] => Text
)
Try this:
$object->_
_ is a valid variable name in PHP.

Categories