getting property name of php object - php

If i have a php object:
stdClass Object
(
[userAttributes] => stdClass Object
(
[dog] => stdClass Object
(
[type] => Canine
[required] => *
[options] =>
[size] =>
)
[cat] => stdClass Object
(
[type] => Feline
[required] => *
[options] =>
[size] =>
)
)
)
without doing a foreach loop, is it possible to print the value of the children of userAttributes. I want to print "dog" and "cat".
if this was an array:
$userAttributes['dog'] = array('type'=>'Canine'.....);
i could do key($userAttributes) and get the word dog. is there a php function that does the same thing as key on objects?

You can use get_object_vars:
$properties = get_object_vars( new yourFunction() );
print_r( $properties );
Also you can try (array) $obj cast to array as:
var_dump((array) $obj);

Try below code:
print_r($yourObject->userAttributes->dog);
EDIT:
print_r(array_keys($yourObject->userAttributes)); // will print dog, cat
http://in3.php.net/array_keys

Related

Search in multidimensional Stdclass PHP

I want to search in PHP within a specific object. For instance [module] => play in a StdClass object, how can I search for values within the class itself?
Below you can find the whole array:
[flow] => stdClass Object
(
[data] => stdClass Object
(
[action] => start
)
[module] => record_call
[children] => stdClass Object
(
[_] => stdClass Object
(
[data] => stdClass Object
(
[id] => 7696364e90822fdcba4feee30574a7e2
[endless_playback] =>
[terminators] => Array
(
[0] => 1
)
)
[module] => play
[children] => stdClass Object
(
[_] => stdClass Object
….
I want to get the output like:
[data] => stdClass Object
(
[id] => 7696364e90822fdcba4feee30574a7e2
[endless_playback] =>
[terminators] => Array
(
[0] => 1
)
)
[module] => play
[children] => stdClass Object
(
[_] => stdClass Object
….
Can someone help me with that? Thank you in advance!
You have a couple of simpler options here.
The first is if you're receiving this data via a json_decode() call, add true as a second parameter:
$decoded = json_decode($data, true);
The second is to convert this to a simple associative array structure by taking advantage of the above and the use of json_encode():
$decoded = json_decode(json_encode($data), true);
With your simple array structure, it's now possible to recursively traverse the array to find the matching key/value pair desired:
function findModule($data, $module_name) {
if(!is_array($data)) return null;
if(array_key_exists('module', $data)) {
if($data['module'] === $module_name) {
return $data;
}
}
foreach($data as $key=>$val) {
$result = findModule($val, $module_name);
if(!is_null($result)) {
return $result;
}
}
return null;
}
// Will either return the array of array('data'=>..., 'module'=>'play', 'children': ...) or null if no matching module found.
$module = findModule($decoded, 'play');

Php combine the values of two array objects

I have a oject that contains:and I want combine the content of [0][locationName and [1][locationName into another object/variable so the content would be "Weymouth and Bournemouth". I know there are similar questions on here but my keys in the array have the same name. How would this be done?
[destination] => stdClass Object
(
[location] => Array
(
[0] => stdClass Object
(
[locationName] => Weymouth
)
[1] => stdClass Object
(
[locationName] => Bournemouth
)
)
)
Many thanks in advance for your time.
Assuming that you have the reference to $destination as an object then perhaps you can try like this:
$arr=$destination->location;
$tmp=array();
foreach( $arr as $index => $obj )$tmp[]=$obj->locationName;
$tmp=implode(' and ',$tmp ); // do something with $tmp
Assuming I recreated the original object correctly
$obj=(object)array(
'destination' => (object)array(
'location' => array(
(object)array('locationName'=>'Weymouth'),
(object)array('locationName'=>'Bournemouth')
)
)
);
The above produces a structure like this:
[destination] => stdClass Object
(
[location] => Array
(
[0] => stdClass Object
(
[locationName] => Weymouth
)
[1] => stdClass Object
(
[locationName] => Bournemouth
)
)
)
$destination=$obj->destination;
pre( $obj );
$arr=$destination->location;
$tmp=array();
foreach( $arr as $index => $obj )$tmp[]=$obj->locationName;
$tmp=implode(' and ',$tmp ); // do something with $tmp
echo $tmp; // --> outputs Weymouth and Bournemouth

Get the value in multidimensional arrays php

How to get values of multidimensional array.
Array is as shown below
Array
(
[id] => 1448639278717703
[birthday] => 06/23/1993
[education] => Array
(
[0] => stdClass Object
(
[school] => stdClass Object
(
[id] => 291422000916149
[name] => Vijeta High School
)
[type] => High School
)
[1] => stdClass Object
(
[school] => stdClass Object
(
[id] => 133445980012001
[name] => Vijnana Vihara Residential School
)
[type] => High School
)
[2] => stdClass Object
(
[concentration] => Array
(
[0] => stdClass Object
(
[id] => 111995945484851
[name] => Electronics
)
)
[school] => stdClass Object
(
[id] => 104121832956302
[name] => Vignan University
)
[type] => College
)
)
)
Let take the array as $graphObject then I tried like shown below
$graphObject['education'][0]['school']['name']
But this doesn't worked.
I want to get
School name and type of it.
Concentration name and its school name
Example:
I have to get like
High school: Vijeta High School
High school: Vijnana Vihara Residential School
Concentration in Electronics at Vignan University
Use below code to convert everything into array -
$array = json_decode(json_encode($array),1);
Where $array is your array.
Your innter array contain an object your acces that value like array.
If you want to solve this issue convert your inner ojbect into array and then access the same that you are using.
It's because your array is an object array,
convert it before with this function:
function objectToArray( $object )
{
if( !is_object( $object ) && !is_array( $object ) )
{
return $object;
}
if( is_object( $object ) )
{
$object = get_object_vars( $object );
}
return array_map( 'objectToArray', $object );
}
then:
$graphObject = objectToArray($graphObject);
print_r ($graphObject['education'][0]['school']['name']);
try
$graphObject['education'][0]->school->name;

convert array of object to associative array

I need to convert below array:-
Array
(
[0] => stdClass Object
(
[id] =>
[risk_reference] =>
[risk_version] =>
[bsi] => 10.00
)
)
to below array:-
Array
(
[id] =>
[risk_reference] =>
[risk_version] =>
[bsi] => 10.00
)
I tried to do it by typecasting. But It didn't give me the output.
I also checked this link
For Above $result = (array)($array[0]) works fine for me.
But if I have the below then what will I do?
Array
(
[0] => stdClass Object
(
[id] =>
[risk_reference] =>
[risk_version] =>
[bsi] => 10.00
)
[1] => stdClass Object
(
[id] =>
[risk_reference] =>
[risk_version] =>
[bsi] => 20.00
)
)
Try this
$array = (array)($array[0]);
try this
$yourArray = array();
$i=0;
foreach ($yourObject as $key => $value) {
$yourArray[$i]['id'] = $value->id;
$yourArray[$i]['risk_reference'] = $value->risk_reference;
$yourArray[$i]['risk_version'] = $value->risk_version;
$yourArray[$i]['bsi'] = $value->bsi;
$i+=1;
}
print_r($yourArray);
http://php.net/get_object_vars
Gets the accessible non-static properties of the given object according to scope.
Returns an associative array of defined object accessible non-static properties for the specified object in scope. If a property has not been assigned a value, it will be returned with a NULL value.

Getting an objects property with slashes in it

I'm getting a json result back from an api request to the freebase database.
This is part of the object returned called $json. A var dump of $json:
stdClass Object
(
[name] => Abomey
[/location/statistical_region/population_growth_rate] =>
[/common/topic/article] => Array
(
[0] => stdClass Object
(
[id] => /m/0jk2c
)
)
How can I subtract the /m/0jk2c part?
$json->/common/topic/article[0]->id (obviously) doesn't work.
This should do it:
$json->{"/common/topic/article"}[0]->id
This is what you should use
$class->{'/location/statistical_region/population_growth_rate'}['/common/topic/article'][0]->id
Reason your Object looks like this
$std = new stdClass();
$std->id = '/m/0jk2c' ;
$json = new stdClass();
$json->name = "Abomey" ;
$json->{'/location/statistical_region/population_growth_rate'} = array('/common/topic/article'=>array($std));
If you run
var_dump($json->{'/location/statistical_region/population_growth_rate'}['/common/topic/article'][0]->id);
Output
string '/m/0jk2c' (length=8)
Run
echo "<pre>";
print_r($json);
Output
stdClass Object
(
[name] => Abomey
[/location/statistical_region/population_growth_rate] => Array
(
[/common/topic/article] => Array
(
[0] => stdClass Object
(
[id] => /m/0jk2c
)
)
)
)

Categories