How to get data from array in object - php

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]

Related

Parsing a timestamp based PHP std class obkect

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;

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.

Accessing an array in a standard class object

I need to access the array coordinates in this:
object stdClass Object (
[type] => Point
[coordinates] => Array (
[0] => 53.64203491
[1] => -8.52337036
)
)
But no matter what I try I can't get it. Any ideas? I am using PHP.
If the name of the variable is $data, then you can access the the coordinates array as follows:-
$coordinatesArr = $data->coordinates;
I think this should work. If it does not, please share the peice of code you are using.
Try something like this: $stdObject->coordinates[0] and $stdObject->coordinates[1].

How to access stdclass object after a specific key value pair?

I have a stdclass object as shown below:
stdClass Object
(
[text] => Parent
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Laurence W. Lane Jr.
[url] => http://www.freebase.com/view/m/0c02911
)
)
)
I iterate over multiple such objects, some of which have
stdClass Object
(
[text] => Named after
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Stanford
[url] => SomeURL
)
)
)
I was wondering how I would access the "values" object if it comes after a "text" that has "Parent" as its value?
there are serveral ways to turn it to array:
First Solution:
$value = get_object_vars($object);
Second Solution:
$value = (array) $object;
Third Solution
$value = json_decode(json_encode($object), true);
to get value of converted array
echo $value['values']['0']['id'];
The alternate way to access objects var without convert the object, try
$object->values->{'0'}->id
Expanding (or rather minimalizing) upon answer by Somwang Souksavatd, I like accessing Object values like this:
echo get_object_vars($object)['values']['0']['id'];
I had the same issue, still not so sure why but I was able to get it working using this workaround:
$k2 ="1";
$elements = json_decode('{"id":"1","name":"User1"}');
//$elements['id'] == $k2; //****Not Working
$tmp = (object)$elements;
$tmp = $tmp ->id; //****Working
//$tmp =$elements['id'] ; //****Not Working
return $tmp == $k2;
I have to say that sometimes accessing the element as array works and some times not,(On PHP7 it worked for me but on PHP5.6 it didn't).
$elements can be Array to but I chose to demonstrate with json string.
I hope this helps somehow !!!
$Obj=stdClass Object
(
[text] => Named after
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Stanford
[url] => SomeURL
)
)
)
$Values= $result->values;
$Item = $Values[0];
$id=$Item->id;
$text = $Item->text;
$url=$Item->url;
I'm doing the same thing and all I did was this;
<?php
$stdObject = json_decode($stdClassObject);
print $stdObject->values[0]->id;
this can help you accessing subarrays in php using codeigniter framework
foreach ($cassule['tarefa'][0] as $tarefa => $novo_puto_ultimos_30_dias) {
echo $novo_puto_ultimos_30_dias;
What you are looking for is the Object['values'][0]: 'values' is the keymap just like 'text', and [0] is the index inside that array you wish to access. so if you would like to get the id deep in the nest, you'd have to do something like
Object['values'][0]['id']
or
Object['values'][0]->id
which should give you /m/0c02911. But I have no idea how you are doing your loop, so you will have to adjust it to your needs and place proper variables where they need to go in that code in your loop. Not exactly sure which language you are working with.

Retrieve value from a multi-dimensional array in PHP

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'];

Categories