簡體   English   中英

無法訪問 std 類對象上的 php stdClass 屬性

[英]Cannot access php stdClass property on std class object

我將值的 ACF std 類對象作為值傳遞給函數,當嘗試在函數中讀取它時,它顯示為一個對象,但是當我嘗試訪問屬性時注意:嘗試獲取屬性“類型”非客體的

function createACFProductLicenses($acfData, $propertyString){
    $newData = array();
    if(isset($acfData->{$propertyString})){
        $data = (array)$acfData->{$propertyString};
        foreach ($data as $key => $value) {

            print_r($value);
            // prints out
            // stdClass Object
            // (
            //   [type] => standard
            //   [item_id] => 727
            // )

            print_r($value->type); // errors out -> Notice: Trying to get property 'type' of non-object
        }
    }
}
// example of how I'm calling it:
// $product['acf'] is an stdClass of acf properties
createACFProductLicenses($product['acf'], 'product_licenses');

“foreach”將數據結構設置為 $key=>$value 或 [type] 是 $key 而“standard”是 $value 所以你不能打印 $value->type 因為對象實際上是 'type' => '標准'。 另外,要知道“foreach”將為對象上的每個項目運行您的代碼,例如 ana 數組,因此當您有不同的 $key 時,您不能只打印特定值。

如果要顯示所有項目,請使用以下命令:

print_r($key.':'.$value) // this will print:  type : standard

或者,如果您只想打印 [Type] 項 試試這個: insted of this

 if(isset($acfData->{$propertyString})){
        $data = (array)$acfData->{$propertyString};
        foreach ($data as $key => $value) {
            print_r($value->type); // errors out -> Notice: Trying to get property 'type' of non-object
        }
    }
}

嘗試這個

if(isset($acfData->{$propertyString})){
        $data = (array)$acfData->{$propertyString};
        print_r($data['type']);
    }
}

我希望這會有所幫助,如果它不起作用,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM