Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法访问std类对象上的php stdClass属性_Php_Wordpress_Advanced Custom Fields - Fatal编程技术网

无法访问std类对象上的php stdClass属性

无法访问std类对象上的php stdClass属性,php,wordpress,advanced-custom-fields,Php,Wordpress,Advanced Custom Fields,我将值的ACF std类对象作为值传递给函数,当尝试在函数中读取它时,它显示为对象,但当我尝试访问属性时,注意:尝试获取非对象的属性“类型” function createACFProductLicenses($acfData, $propertyString){ $newData = array(); if(isset($acfData->{$propertyString})){ $data = (array)$acfData->{$property

我将值的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”=>“standard”。 另外,要知道“foreach”将为对象上的每个项目运行代码,就像为ana数组一样,因此当您有不同的$key时,您不能只打印特定的值

如果要显示所有项目,请使用以下选项:

print_r($key.':'.$value) // this will print:  type : standard
或者,如果只想打印[Type]项,请尝试以下操作: 相反

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

我希望这会有所帮助,如果不起作用,请告诉我。

谢谢你的帮助!看起来我需要用一张空支票替换isset,然后一切正常。我很欣赏关于如何正确打印数据的见解。光是这一点就值得投赞成票!