user3422501
user3422501

Reputation: 145

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

  1. School name and type of it.
  2. 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

Upvotes: 1

Views: 62

Answers (4)

Viswanath Polaki
Viswanath Polaki

Reputation: 1402

try

$graphObject['education'][0]->school->name;

Upvotes: 0

Anik
Anik

Reputation: 471

Use below code to convert everything into array -

$array = json_decode(json_encode($array),1);

Where $array is your array.

Upvotes: 1

Sebastien Horin
Sebastien Horin

Reputation: 11069

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

Upvotes: 0

Yatin Mistry
Yatin Mistry

Reputation: 1264

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.

Upvotes: 0

Related Questions