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;
Related
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
I'm trying to sort the following data by the date in the key and the value of Name.
The aim is to a get nice date ordered array with all the Names from the inner array in alphabetical order.
Array
(
[2017-07-27] => Array
(
[0] => stdClass Object
(
[Job] => stdClass Object
(
[Name] => Orange
)
)
[4] => stdClass Object
(
[Job] => stdClass Object
(
[Name] => Apple
)
)
)
[2017-07-22] => Array
(
[6] => stdClass Object
(
[Job] => stdClass Object
(
[Name] => Apple
)
)
[7] => stdClass Object
(
[Job] => stdClass Object
(
[Name] => Orange
)
)
)
[2017-07-29] => Array
(
[9] => stdClass Object
(
[Job] => stdClass Object
(
[Name] => Orange
)
)
[11] => stdClass Object
(
[Job] => stdClass Object
(
[Name] => Plumb
)
)
)
)
I'm pretty sure I should be using array_multisort but can't quite get the desired results.
You must split the code if you want to order on object properties, use the usort function.
Where $arr is your array:
uksort($arr, 'dateCmp');
foreach($arr as &$sub){
usort($sub, 'propCmp');
}
function dateCmp($a, $b){
return (strtotime($a) < strtotime($b) ? -1 : 1);
}
function propCmp($a, $b){
return ($a->Job->Name < $b->Job->Name ? -1 : 1);
}
Please try below code,
$sorted_vals = array();
ksort($multiArrs);
foreach($multiArrs as $key => $value) { // $multiArrs = your data array
$columns = null;
foreach ($value as $index => $element) {
$columns[] = $element->Job;
}
$temp = $value;
array_multisort($columns, SORT_ASC, $temp);
$sorted_vals[$key] = $temp;
}
How do I turn this:
Array
(
[0] => stdClass Object
(
[id] => 123
[name] => Board
)
[1] => stdClass Object
(
[id] => 133
[name] => Staff
)
)
Into:
Array
(
[0] => stdClass Object
(
[id] => 133
[name] => Staff
)
[1] => stdClass Object
(
[id] => 123
[name] => Board
)
)
Based on this:
Array( 'Staff', 'Board' )
In other words -- order an array of objects based on an array of values...
$ordering = ['Staff', 'Board'];
usort($myObjects, function($a, $b) use ($ordering) {
$idx1 = array_search($a->name, $ordering);
$idx2 = array_search($b->name, $ordering);
if($idx1 == $idx2)
return 0;
elseif($idx1 < $idx2)
return -1;
return 1;
});
This example does assume that $ordering will contain all names it's going to encounter. If not you'll have to patch behaviour in there (can't do that because I don't know 'where' you want the unmatched items to sort to).
This question already has answers here:
stdClass object and foreach loops
(5 answers)
Closed 4 months ago.
I have an object like this:
stdClass Object
(
[_count] => 10
[_start] => 0
[_total] => 37
[values] => Array
(
[0] => stdClass Object
(
[_key] => 50180
[group] => stdClass Object
(
[id] => 50180
[name] => CriticalChain
)
)
[1] => stdClass Object
(
[_key] => 2357895
[group] => stdClass Object
(
[id] => 2357895
[name] => Data Modeling
)
)
[2] => stdClass Object
(
[_key] => 1992105
[group] => stdClass Object
(
[id] => 1992105
[name] => SQL Server Users in Israel
)
)
[3] => stdClass Object
(
[_key] => 37988
[group] => stdClass Object
(
[id] => 37988
[name] => CDO/CIO/CTO Leadership Council
)
)
[4] => stdClass Object
(
[_key] => 4024801
[group] => stdClass Object
(
[id] => 4024801
[name] => BiT-HR, BI & IT Placement Agency
)
)
[5] => stdClass Object
(
[_key] => 37845
[group] => stdClass Object
(
[id] => 37845
[name] => Israel Technology Group
)
)
[6] => stdClass Object
(
[_key] => 51464
[group] => stdClass Object
(
[id] => 51464
[name] => Israel DBA's
)
)
[7] => stdClass Object
(
[_key] => 66097
[group] => stdClass Object
(
[id] => 66097
[name] => SQLDBA
)
)
[8] => stdClass Object
(
[_key] => 4462353
[group] => stdClass Object
(
[id] => 4462353
[name] => Israel High-Tech Group
)
)
[9] => stdClass Object
(
[_key] => 4203807
[group] => stdClass Object
(
[id] => 4203807
[name] => Microsoft Team Foundation Server
)
)
)
)
I need to get the id and name in an HTML table, but I seem to have a hard time iterating through this object. TIA. I understand that I need to get to the Values Array, and then to the group object, but I trip over the transitions between object and array and foreach vs index based iteration.
For example I tried this:
foreach ($res as $values) { print "\n"; print_r ($values); }
It iterates trough the object, but it also gives me useless
10 0 37
echo "<table>"
foreach ($object->values as $arr) {
foreach ($arr as $obj) {
$id = $obj->group->id;
$name = $obj->group->name;
$html = "<tr>";
$html .= "<td>Name : $name</td>";
$html .= "<td>Id : $id</td>";
$html .= "</tr>";
}
}
echo "</table>";
Since this is the top result in Google if you search for iterate over stdclass it may be helpful to answer the question in the title:
You can iterate overa stdclass simply by using foreach:
$user = new \stdClass();
$user->flag = 'red';
foreach ($user as $key => $value) {
// $key is `flag`
// $value is `red`
}
function objectToArray( $data )
{
if ( is_object( $data ) )
$d = get_object_vars( $data );
}
Convert the Object to array first like:
$results = objectToArray( $results );
and use
foreach( $results as result ){... ...}
I know it's an old post , but for sake of others:
when working with stdClass you should use Reflections:
$obj = new ReflectionObject($object);
$propeties = $obj->getProperties();
foreach($properties as $property) {
$name = $property->getName(); <-- this is the reflection class
$value = $object->$name; <--- $object is your original $object
here you need to handle the result (store in array etc)
}
foreach($res->values as $value) {
print_r($value);
}
I have a multidimensional array like the following:
Array (
[0] => stdClass Object (
[name] => StackOverflow
[image] => CanHelp.jpg
)
[1] => stdClass Object (
[name] => AnotherObject
[image] => SecondImage.jpg
)
)
How can I arrange/split this array into groups based on the first letter of [name]?
i.e. There are about 1,000 items in this array, which I have already ordered alphabetically by [name], however I want to be able to have groups that begin with 'A', 'B', etc.
Like this, for 'A' and 'S':
Array (
[0] => stdClass Object (
[name] => AnotherObject
[image] => SecondImage.jpg
)
[1] => stdClass Object (
[name] => AndAnother
[image] => notImportant.jpg
)
)
Array (
[0] => stdClass Object (
[name] => StackOverflow
[image] => CanHelp.jpg
)
)
$split = array();
foreach ($array as $item) {
$split[$item->name[0]][] = $item;
}