2016-05-01 14 views
1

Ich erhalte einen Fehler beim Zugriff auf Elemente eines Arrays.PHP: Objekt des Typs stdClass kann nicht als Array verwendet werden

Dies ist meine aktuellen Code:

1. Methode ::

var_dump($parent_array->info->gcatname); 

Error (erste Methode) ::

<b>Notice</b>: Trying to get property of non-object 

zweite Methode ::

print_r($parent_array[0]['info']['gcatname']); 

Fehler (2. Methode) ::

<b>Fatal error</b>: Cannot use object of type stdClass as array 

Array ist wie folgt:

array(1) { 
[0]=> 
array(2) { 
    ["is_parent"]=> 
    bool(true) 
    ["info"]=> 
    object(stdClass)#6 (5) { 
    ["id"]=> 
    string(1) "1" 
    ["gcatname"]=> 
     string(9) "Swine Flu" 
    ["gcatowner"]=> 
     string(13) "Vaccine India" 
    ["gcatactive"]=> 
     string(1) "1" 
    ["gcatadded"]=> 
     string(19) "2016-05-01 08:30:36" 
    } 
    } 
} 
+3

Eckige Klammern ('[]') und der Schlüssel für Arrays; '->' für Objekteigenschaften: 'print_r ($ parent_array [0] ['info'] -> gcatname);' –

Antwort

2

einfach: $parent_array[0]['info']->gcatname

array(1) { 
[0]=> // array(2) stands for the fact that this element with index 0 is an array with the size '2' and it can only be accesses using [] 
array(2) { 
    ["is_parent"]=> 
    bool(true) 
    ["info"]=>// object(stdClass) stands for the fact that this element with index 'info' is an array with the size '5' and it can be accesses using ['info'] 
    object(stdClass)#6 (5) {// here you have accessed the object now when you wish to access inside this scope you need to use this -> 
    ["id"]=> 
    string(1) "1" 
    ["gcatname"]=>//by using ->gcatname you access the property gcatname of the object 
     string(9) "Swine Flu" 
    ["gcatowner"]=> 
     string(13) "Vaccine India" 
    ["gcatactive"]=> 
     string(1) "1" 
    ["gcatadded"]=> 
     string(19) "2016-05-01 08:30:36" 
    } 
    } 
} 
+0

Hoppla! so einfach .. können Sie mir sagen, wie können wir wissen Zugang zu Array-Werte ... wie Sie nur durch das Array gerade sehen, .. – Gags

+1

Sicher halten, ich werde meine Frage bearbeiten –

+1

@Gags Ich habe edited Sie überprüfen können –

Verwandte Themen