I have the following class:
stdClass Object
(
[#attributes] => Array
(
[bla] => 1122
)
[element] => Array
(
[bla] => 1122
)
)
I want to reference the #attributes part of the class. It's easy enough to do it for the "element" one... that would be:
$class->element['bla']
But the same isn't true for the attribute one. The following doesn't work:
$class->#attributes['bla']
$class->"#attributes"['bla']
$class->(#attributes)['bla']
How do I call that element?
do
echo $class->{'#attributes'}['bla'];
//or
$name = '#attributes';
echo $class->$name['bla'];
You can do it like this:
$varname = '#attributes';
print_r($object->$varname);
Related
I have an array looking something like this:
Array
(
[0] => my_val_one
[1] => my_val_two
)
I then have an object looking something like this:
stdClass Object
(
[id] => 123123
[name] => my_name
[my_val_one] => stdClass Object
(
[my_val_two] => 1
[my_val_three] => 2323
[my_val_four] => 546567
)
)
I want to reference the following object value:
$ob->my_val_one->my_val-two
I'm not sure how to reference this class property from array values that I have.
array_reduce helps here:
$path = ['my_val_one', 'my_val_two'];
$value = array_reduce($path, function ($o, $p) { return $o->$p; }, $ob);
If I have understood correctly, you want to use the string values of the first array to access the StdClass object. You can do this by accessing the attributes dynamically. Here $obj is the StdClass and $arr is your array.
$obj->{$arr[0]}->{$arr[1]}
I have another (may stupid) question:
How can I get the "Olaf" (or everything else what is there) from the meta in this array in a $variable ?
Array (
[0] => Pagekit\Blog\Model\Post Object (
[id] => 1
DateTimeObject (
[bla] => Bla
)
Pagekit\User\Model\User Object (
[bla] => bla
)
[meta] => Array (
[og:description] => Olaf
)
)
)
Thanks for your help.
I see meta is a key under object of [0]th key of main array. You should be able to get the values using ->.
$var1 = $test[0]->meta["og:description"];
Try this:
$meta_desc = $arr[0]->meta['og:description']
I understand that first element of array is an object.
I have a class like so:
stdClass Object
(
[id] => 1
[items] => stdClass Object
(
[0] => 123
[1] => 234
[2] => 345
[3] => 456
)
)
)
Let's call the above object $foo.
Let's say $v = 234. Given $foo and $v, how can I return the "key" 1?
If $foo->items was an array I would simply do $key = array_search($v,$foo->items);. But this doesn't work in an object.
How can I find the key for $v without looping through the object in some foreach?
Use get_object_vars and search through the array returned.
Reference: http://php.net/manual/en/function.get-object-vars.php
Here's an example of how to search through the array returned for a key:
<?php
$foo = NULL;
$foo->id = 1;
$foo->items = array(123, 234, 345, 456);
$foo_array = get_object_vars($foo);
print_r($foo_array);
foreach ($foo_array as $key => $value) {
if ($value == 1) {
echo $key;
}
}
?>
Output:
Array
(
[id] => 1
[items] => Array
(
[0] => 123
[1] => 234
[2] => 345
[3] => 456
)
)
id
CodePad: http://codepad.org/in4w94nG
As you have shown in your example, you're dealing with stdClass object(s). Those are quite similar to arrays and with PHP you can easily convert between those two with something called casting:
$object = $foo->items;
$key = array_search($v, (array)$object);
^^^^^^^--- cast to array
As this little example shows (I just used the $object variable to make the cast more visible, you normally can write it as one-liner), the cast from object to array does allow you to use the known function (array_search) on the object.
Because arrays and stdClass objects in PHP are so similar, this works in both directions:
$array = ['property' => 'value'];
$object = (object)$array;
echo $object->property; # value
This also works with other types in PHP, so probably apart from your concrete problem, something worth to read about: Type JugglingÂDocs, but take care, that in PHP this has many special rules. But between array and objects, it's pretty straight forward.
$key = array_search($v, get_object_vars($foo->items));
edit: try this
When I print_r($var) I get the result below.
SimpleXMLElement Object
(
[SEND_FILE] => SimpleXMLElement Object
(
[FILEID] => 123
[GUID] => 456
[SUMMARY] => SimpleXMLElement Object
(
[NB_PAYMENTS] => 1
)
)
)
How can I get the value of the FILEID element in a variable? If I do
print $result->SEND_FILE->FILEID[0]
then I just get the number - what I want, no mention of a SimpleXML Object.
But if I put this variable in an array, as such
$res['file_id'] = $result->SEND_FILE->FILEID[0]
and then print_r($res) I get:
Array
(
[file_id] => SimpleXMLElement Object
(
[0] => 307466
)
)
How can I get it to remove the [0] / SimpleXMLElement Object?
This will look not too elegant, but try casting the result to integer (if the type is known):
$res['file_id'] = (int)$result->SEND_FILE->FILEID[0]
Why do you append the [0] at the end? You dont need that. You should simply do
print $result->SEND_FILE->FILEID;
And that should be enough.
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.