2016-05-01 19 views
1

配列の要素にアクセスする際にエラーが発生します。PHP:配列として型stdClassのオブジェクトを使用できません

これは私の現在のコードです:

第一の方法::

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

エラー(第一の方法)::

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

第二の方法::

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

エラー(第2方法)::

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

配列は以下の通りである:単に

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

角括弧( '[]')配列のキー。 ' - >'オブジェクトのプロパティ: 'print_r($ parent_array [0] ['info'] - > gcatname);' –

答えて

2

$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

おっと!アレイの値を参照するだけで投稿されたように、配列値にアクセスする方法を知る方法を教えてください。 – Gags

+1

私の質問を編集します。 –

+1

@Gags私はあなたがチェックできるように編集しました –

関連する問題