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]}
Related
I am getting a JSON response which looks like this:
stdClass Object
(
[location00] => Array
(
[0] => stdClass Object
(
[id_0] => Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Wanted by Aryurumoka
[gold_reward] => 58900
[event] => 0
[description] => Not provided.
)
)
)
)
)
For example, i am able to get [name] by $quests->location00[0]->id_0[0]->name.
Lets say i create a new variable $location = 'location00'. Now if i try $quests->$location[0]->id_0[0]->name', i am getting Undefined property: stdClass::$l error. I tried $location = 'location00[0]' as well however i have completly no idea why this happenes. How can i assign location00 to variable to use it while parsing JSON?
You can use associative array or $obj->{$var} :
<?php
$quests = json_decode($json, true);
$location = 'location00';
$name = $quests[$location][0]['id_0'][0]['name'];
I'd try to get new JSON, but you can interpolate object property retrieval with braces:
$quests->{$location}[0]
I have this simple array $tree in PHP that I need to filter based on an array of tags matching those in the array.
Array
(
[0] => stdClass Object
(
[name] => Introduction
[id] => 798162f0-d779-46b6-96cb-ede246bf4f3f
[tags] => Array
(
[0] => client corp
[1] => version 2
)
)
[1] => stdClass Object
(
[name] => Chapter one
[id] => 761e1909-34b3-4733-aab6-ebef26d3fcb9
[tags] => Array
(
[0] => pro feature
)
)
)
I tried using an anonymous function like so:
$selectedTree = array_filter($tree, function($array) use ($selectedTags){
return in_array($array->tags, $selectedTags, true);
});
$selectedTags:
Array
(
[0] => client corp
)
The above is returning empty when I'd expect item 1 to be returned. No error thrown. What am I missing?
In case of in_array($neddle, $haystack). the $neddle must need to be a String, but you're giving an array that is why its not behaving properly.
But if you like to pass array as value of $selectedTags then you might try something like below:
$selectedTree = array_filter($tree, function($array) use ($selectedTags){
return count(array_intersect($array->tags, $selectedTags)) > 0;
});
Ref: array_intersect
If I am reading the question correctly, you need to look at each object in $tree array and see if the tags property contains any of the the elements in $selectedTags
Here is a procedural way to do it.
$filtered = array();
foreach ($tree as $key => $obj) {
$commonElements = array_intersect($selectedTags, $obj->tags);
if (count($commonElements) > 0) {
$filtered[$key] = $obj;
}
}
I was going to also post the functional way of doing this but, see thecodeparadox's answer for that implementation.
I have the following result
Array (
[0] => stdClass Object ( [name] => Identification )
[1] => stdClass Object ( [name] => Assay )
[2] => stdClass Object ( [name] => pH(Acidity/Alkalinity))
[3] => stdClass Object ( [name] => Sterility )
)
What i want is to separate the object array values using a comma and return as a string,
so as to have this result:
Identification, Assay, ph(Acid/Alkalinity), Sterility
I have tried the following
$data=(array)$result;
$answer=implode(",",$data);
This return :
Message: Object of class stdClass could not be converted to string
How best can this be achieved?
You are missing the fact that you are dealing with an array of objects.
Looks like you can achieve that by doing:
$output = array_map(function ($object) { return $object->name; }, $input);
echo implode(', ', $output);
To use this feature in the 'title' attribute I did:
echo implode('
', $output);
It will convert the array of objects to a string, and you can store it in the database, use
serialize(array_of_object)
Might help someone. I used it in my case. Thank you!
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);
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.