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.
Related
I have an array like below.
Here, I am getting array key like [_aDeviceTokens:protected] => Array.
$array= ApnsPHP_Message Object
(
[_bAutoAdjustLongPayload:protected] => 1
[_aDeviceTokens:protected] => Array
(
[0] => BD74940085E1579333E93B7D172CF82F5A3E0B17617D904107CD77573C42CEC9
)
[_sText:protected] => test
[_nBadge:protected] => 1
[_sSound:protected] => default
[_sCategory:protected] =>
[_bContentAvailable:protected] =>
[_aCustomProperties:protected] => Array
(
[channel_id] => 1xxxx8
[detail_id] => 1
)
[_nExpiryValue:protected] => 1500
[_mCustomIdentifier:protected] =>
)
As an array have object value so I am trying to get value of this key like,
$array->_aDeviceTokens:protected[0]
But this gives me an error.
So how can I achieve the value of these array keys?
It seems you are trying to access protected properties of an object that you are treating like an array.
Looking at the code here: https://github.com/immobiliare/ApnsPHP/blob/master/ApnsPHP/Message.php
There are publically accessible 'getters' for those attributes.
Extract of class ApnsPHP_Message:
public function getCustomIdentifier()
{
return $this->_mCustomIdentifier;
}
So instead of trying to access those properties as you have been, use the corresponding getter.
$custom_identifier = $message->getCustomIdentifier();
Convert object to array.
$array = json_decode(json_encode($array),true);
Now, you get value from $array like this
$array['_aDeviceTokens:protected'][0]
I have a PHP standard class object converted from json_decode of a REST call on an API which looks like :
Array
(
[1437688713] => stdClass Object
(
[handle] => Keep it logically awesome.
[id] => 377748
[ping] => stdClass Object
(
[url] => https://api.me.com
[id] => 377748
[name] => web
[active] => 1
[events] => Array
(
[0] => data_new
[1] => data_old
)
So far i had no issues in parsing any of the PHP objects. However this one is failing because i can not access the nested object elements using a key since 1437688713 is not assigned to a key and accessing an object is failing if i try to do this:
$object->1437688713->handle
Is there a way to access these elements ?
Update: one more thing, i would never know this value (1437688713) in advance. Just like a key. All i get is a stdclass object which i have to parse.
The outer part of your data is an array, not an object. Try:
$array['1437688713']->handle;
or if you don't know the key, you can iterate over the array (handy if it may contain multiple objects too):
foreach ($array as $key => $object) {
echo $key; // outputs: 1437688713
echo $object->handle; // outputs: Keep it logically awesome.
}
Get the first item from $object array
$first_key = key($object);
Use it with your response array,
$object[$first_key]->handle;
Or, the first element of array
$first_pair = reset($object)->handle;
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]
I have a multidim array that I can view using print_r($users):
Array
(
[0] => stdClass Object
(
[username] => crustacea
[created_on] => 2010-08-07 19:54:32
[active] => 1
)
)
I can also use print_r($users[0]). Since there's only one member in $users, the output looks somewhat the same.
I can't see how to retrieve the value crustacea. echo $users[0]['username']; doesn't do it. Examples I can find are echo $users['name_of_array']['username']; -- but I don't have a name of array to work with. How do I do it?
$user[0] is not an array but an object as indicated by [0] => stdClass Object.
echo $users[0]->username;
$user[0] is object and not an array. You can access your value by doing
$users[0]->username;
Or you could cast/convert your object into an array like
((array)$users[0])['username'];
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;