如何从 PHP 对象中提取详细信息?

发布于 2024-10-07 01:58:34 字数 7817 浏览 0 评论 0原文

我正在使用一个以如下格式返回信息的对象...我删除了一些细节,其中信息有点重复以节省一些空间。

ISC_QUOTE_ITEM Object (
    [type:protected] => 1
    [id:protected] => 4cff5974138c9
    [hash:protected] => cd2a8393493a286cf1fe97d92a947719
    [productData:protected] => 
    [productId:protected] => 28
    [variationId:protected] => 0
    [variationOptions:protected] => Array ( )
    [quantity:protected] => 1
    [originalOrderQuantity:protected] => 0
    [name:protected] => Mac Pro
    [basePrice:protected] => 2799.0000
    [sku:protected] => 
    [wrapping:protected] => Array ( )
    [configuration:protected] => Array ( )
    [eventDate:protected] => Array ( )
    [eventName:protected] => 
    [parentId:protected] => 
    [quote:protected] => ISC_QUOTE Object (
        [customerId:protected] => 
        [customerGroupId:protected] => 
        [discounts:protected] => Array ( ) 
        [addresses:protected] => Array ( 
            [4cff2f55b0fcd] => ISC_QUOTE_ADDRESS_SHIPPING Object (
                [shippingMethod:protected] => 
                [shippingCache:protected] => 
                [handlingCost:protected] => 0 
                [quote:protected] => ISC_QUOTE Object *RECURSION*
                [type:protected] => shipping
                [id:protected] => 4cff2f55b0fcd
                [firstName:protected] => asd
                [lastName:protected] => asd
                [company:protected] => asd
                [phone:protected] => asd
                [email:protected] => [email protected]
                [address1:protected] => asd
                [address2:protected] => 
                [zip:protected] => 62990
                [city:protected] => asd
                [state:protected] => Array (
                    [id] => 0
                    [iso2] => 
                    [name] => asd
                )
                [country:protected] => Array (
                    [id] => 99
                    [name] => India
                    [iso2] => IN
                )
                [cachedTotals:protected] => Array ( )
                [customFields:protected] => Array ( )
                [saveAddress:protected] => 
                [customerAddressId:protected] => 0
            )
            [4cff2f55cc2b6] => ISC_QUOTE_ADDRESS Object (
                [handlingCost:protected] => 
                [quote:protected] => ISC_QUOTE Object *RECURSION*
                [type:protected] => billing
                [id:protected] => 4cff2f55cc2b6
                [firstName:protected] => asd
                [lastName:protected] => asd
                [company:protected] => asd
                [phone:protected] => asd
                [email:protected] => [email protected]
                [address1:protected] => asd
                [address2:protected] => 
                [zip:protected] => 62990
                [city:protected] => asd
                [state:protected] => Array (
                    [id] => 0
                    [iso2] => 
                    [name] => asd
                )
                [country:protected] => Array (
                    [id] => 99
                    [name] => India
                    [iso2] => IN
                )
                [cachedTotals:protected] => Array ( )
                [customFields:protected] => Array ( )
                [saveAddress:protected] => 
                [customerAddressId:protected] => 0
            )
        )
        [coupons:protected] => Array ( )
        [giftCertificates:protected] => Array ( )
        [items:protected] => Array (
            [0] => ISC_QUOTE_ITEM Object (
                [type:protected] => 1
                [id:protected] => 4cff2f55b11d2
                [hash:protected] => 587e0ee453488554f782b20f3c928916
                [productData:protected] => 
                [productId:protected] => 24
                [variationId:protected] => 0
                [variationOptions:protected] => Array ( )
                [quantity:protected] => 3
                [originalOrderQuantity:protected] => 0
                [name:protected] => Apple iPod Socks
                [basePrice:protected] => 29.0000
                [sku:protected] => 
                [wrapping:protected] => Array ( )
                [configuration:protected] => Array ( )
                [eventDate:protected] => Array ( )
                [eventName:protected] => 
                [parentId:protected] => 
                [quote:protected] => ISC_QUOTE Object *RECURSION*
                [discounts:protected] => Array ( )
                [isCustomPrice:protected] => 
                [addressId:protected] => 4cff2f55b0fcd
                [cachedTotals:protected] => Array ( )
                [inQuote:protected] => 1
                [inventoryChecking:protected] => 1
                [weight:protected] => 1.0000
                [fixedShippingCost:protected] => 0.0000
            )
            [1] => ISC_QUOTE_ITEM Object (
                [type:protected] => 1
                [id:protected] => 4cff58167d29f
                [hash:protected] => 1da6aa898763172b8f7b9d8097d37f42
                [productData:protected] => 
                [productId:protected] => 23
                [variationId:protected] => 0
                [variationOptions:protected] => Array ( )
                [quantity:protected] => 1
                [originalOrderQuantity:protected] => 0
                [name:protected] => Apple In-Ear Headphones with Remote and Mic
                [basePrice:protected] => 79.0000
                [sku:protected] => 
                [wrapping:protected] => Array ( )
                [configuration:protected] => Array ( )
                [eventDate:protected] => Array ( )
                [eventName:protected] => 
                [parentId:protected] => 
                [quote:protected] => ISC_QUOTE Object *RECURSION*
                [discounts:protected] => Array ( )
                [isCustomPrice:protected] => 
                [addressId:protected] => 4cff2f55b0fcd
                [cachedTotals:protected] => Array ( )
                [inQuote:protected] => 1
                [inventoryChecking:protected] => 1
                [weight:protected] => 2.0000
                [fixedShippingCost:protected] => 0.0000
            )
            [2] => ISC_QUOTE_ITEM Object *RECURSION*
        )
        [isSplitShipping:protected] =>
        [cachedTotals:protected] => Array ( )
        [hasFreeShipping:protected] => 
        [appliedStoreCredit:protected] => 0
        [appliedDiscountRules:protected] => Array ( )
        [customerMessage:protected] => 
        [staffNotes:protected] => 
        [orderStatus:protected] => 0
        [orderId:protected] => 
        [discountsEnabled:protected] => 1
    )
    [discounts:protected] => Array ( )
    [isCustomPrice:protected] => 
    [addressId:protected] => 4cff2f55b0fcd
    [cachedTotals:protected] => Array ( )
    [inQuote:protected] => 1
    [inventoryChecking:protected] => 1
    [weight:protected] => 3.0000
    [fixedShippingCost:protected] => 0.0000
) 

我需要提取 ID、名称等详细信息,以便它分别读取 4cff5974138c9Mac Pro 等。我怎样才能做到这一点?

I'm working with an object that returns information in a format like the following... I've removed some details where the information gets a little repetitive to save some space.

ISC_QUOTE_ITEM Object (
    [type:protected] => 1
    [id:protected] => 4cff5974138c9
    [hash:protected] => cd2a8393493a286cf1fe97d92a947719
    [productData:protected] => 
    [productId:protected] => 28
    [variationId:protected] => 0
    [variationOptions:protected] => Array ( )
    [quantity:protected] => 1
    [originalOrderQuantity:protected] => 0
    [name:protected] => Mac Pro
    [basePrice:protected] => 2799.0000
    [sku:protected] => 
    [wrapping:protected] => Array ( )
    [configuration:protected] => Array ( )
    [eventDate:protected] => Array ( )
    [eventName:protected] => 
    [parentId:protected] => 
    [quote:protected] => ISC_QUOTE Object (
        [customerId:protected] => 
        [customerGroupId:protected] => 
        [discounts:protected] => Array ( ) 
        [addresses:protected] => Array ( 
            [4cff2f55b0fcd] => ISC_QUOTE_ADDRESS_SHIPPING Object (
                [shippingMethod:protected] => 
                [shippingCache:protected] => 
                [handlingCost:protected] => 0 
                [quote:protected] => ISC_QUOTE Object *RECURSION*
                [type:protected] => shipping
                [id:protected] => 4cff2f55b0fcd
                [firstName:protected] => asd
                [lastName:protected] => asd
                [company:protected] => asd
                [phone:protected] => asd
                [email:protected] => [email protected]
                [address1:protected] => asd
                [address2:protected] => 
                [zip:protected] => 62990
                [city:protected] => asd
                [state:protected] => Array (
                    [id] => 0
                    [iso2] => 
                    [name] => asd
                )
                [country:protected] => Array (
                    [id] => 99
                    [name] => India
                    [iso2] => IN
                )
                [cachedTotals:protected] => Array ( )
                [customFields:protected] => Array ( )
                [saveAddress:protected] => 
                [customerAddressId:protected] => 0
            )
            [4cff2f55cc2b6] => ISC_QUOTE_ADDRESS Object (
                [handlingCost:protected] => 
                [quote:protected] => ISC_QUOTE Object *RECURSION*
                [type:protected] => billing
                [id:protected] => 4cff2f55cc2b6
                [firstName:protected] => asd
                [lastName:protected] => asd
                [company:protected] => asd
                [phone:protected] => asd
                [email:protected] => [email protected]
                [address1:protected] => asd
                [address2:protected] => 
                [zip:protected] => 62990
                [city:protected] => asd
                [state:protected] => Array (
                    [id] => 0
                    [iso2] => 
                    [name] => asd
                )
                [country:protected] => Array (
                    [id] => 99
                    [name] => India
                    [iso2] => IN
                )
                [cachedTotals:protected] => Array ( )
                [customFields:protected] => Array ( )
                [saveAddress:protected] => 
                [customerAddressId:protected] => 0
            )
        )
        [coupons:protected] => Array ( )
        [giftCertificates:protected] => Array ( )
        [items:protected] => Array (
            [0] => ISC_QUOTE_ITEM Object (
                [type:protected] => 1
                [id:protected] => 4cff2f55b11d2
                [hash:protected] => 587e0ee453488554f782b20f3c928916
                [productData:protected] => 
                [productId:protected] => 24
                [variationId:protected] => 0
                [variationOptions:protected] => Array ( )
                [quantity:protected] => 3
                [originalOrderQuantity:protected] => 0
                [name:protected] => Apple iPod Socks
                [basePrice:protected] => 29.0000
                [sku:protected] => 
                [wrapping:protected] => Array ( )
                [configuration:protected] => Array ( )
                [eventDate:protected] => Array ( )
                [eventName:protected] => 
                [parentId:protected] => 
                [quote:protected] => ISC_QUOTE Object *RECURSION*
                [discounts:protected] => Array ( )
                [isCustomPrice:protected] => 
                [addressId:protected] => 4cff2f55b0fcd
                [cachedTotals:protected] => Array ( )
                [inQuote:protected] => 1
                [inventoryChecking:protected] => 1
                [weight:protected] => 1.0000
                [fixedShippingCost:protected] => 0.0000
            )
            [1] => ISC_QUOTE_ITEM Object (
                [type:protected] => 1
                [id:protected] => 4cff58167d29f
                [hash:protected] => 1da6aa898763172b8f7b9d8097d37f42
                [productData:protected] => 
                [productId:protected] => 23
                [variationId:protected] => 0
                [variationOptions:protected] => Array ( )
                [quantity:protected] => 1
                [originalOrderQuantity:protected] => 0
                [name:protected] => Apple In-Ear Headphones with Remote and Mic
                [basePrice:protected] => 79.0000
                [sku:protected] => 
                [wrapping:protected] => Array ( )
                [configuration:protected] => Array ( )
                [eventDate:protected] => Array ( )
                [eventName:protected] => 
                [parentId:protected] => 
                [quote:protected] => ISC_QUOTE Object *RECURSION*
                [discounts:protected] => Array ( )
                [isCustomPrice:protected] => 
                [addressId:protected] => 4cff2f55b0fcd
                [cachedTotals:protected] => Array ( )
                [inQuote:protected] => 1
                [inventoryChecking:protected] => 1
                [weight:protected] => 2.0000
                [fixedShippingCost:protected] => 0.0000
            )
            [2] => ISC_QUOTE_ITEM Object *RECURSION*
        )
        [isSplitShipping:protected] =>
        [cachedTotals:protected] => Array ( )
        [hasFreeShipping:protected] => 
        [appliedStoreCredit:protected] => 0
        [appliedDiscountRules:protected] => Array ( )
        [customerMessage:protected] => 
        [staffNotes:protected] => 
        [orderStatus:protected] => 0
        [orderId:protected] => 
        [discountsEnabled:protected] => 1
    )
    [discounts:protected] => Array ( )
    [isCustomPrice:protected] => 
    [addressId:protected] => 4cff2f55b0fcd
    [cachedTotals:protected] => Array ( )
    [inQuote:protected] => 1
    [inventoryChecking:protected] => 1
    [weight:protected] => 3.0000
    [fixedShippingCost:protected] => 0.0000
) 

I need to extract details such as ID, name, etc. so that it reads 4cff5974138c9, Mac Pro, etc. respectively. How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

私藏温柔 2024-10-14 01:58:34

您需要调用该类的函数(其中 ISC_QUOTE_ITEM 是一个对象 - 比如说 MyClass ),该函数返回受保护的变量

Edit
可以访问它。我是对的。

像这样 -

如果您还没有一个函数,请在您的类 MyClass 中添加一个函数。

public function getName()
{
  return $this->name;
}

然后在您的代码中您可以执行以下操作:-

echo $obj->getName();

阅读 http://php.net /manual/en/language.oop5.visibility.php 有关变量类型可见性的详细信息

You need to call a function of that class (of which ISC_QUOTE_ITEM is an object - say MyClass ) which returns the protected variable

Edit
It can be accessed. I was correct.

Like this -

Add a function inside your class MyClass if you don't already have one.

public function getName()
{
  return $this->name;
}

And then in your code you can do this:-

echo $obj->getName();

Read http://php.net/manual/en/language.oop5.visibility.php for details on visibility of types of variables

ペ泪落弦音 2024-10-14 01:58:34

您可以使用反射器构造来完成此操作:

示例取自 http://www. php.net/manual/en/reflectionclass.getproperties.php

<?php
class Foo {
    public    $foo  = 1;
    protected $bar  = 2;
    private   $baz  = 3;
}

$foo = new Foo();

$reflect = new ReflectionClass($foo);
$props   = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);

foreach ($props as $prop) {
    print $prop->getName() . "\n";
}

var_dump($props);

?>

正如您所看到的,还返回了受保护的 $bar :) 并查看 http://www.php.net/manual/en/reflectionproperty.getvalue.php#98643 此处显示了如何返回受保护属性的值。

You can do this by using the reflector construct:

Example taken from http://www.php.net/manual/en/reflectionclass.getproperties.php:

<?php
class Foo {
    public    $foo  = 1;
    protected $bar  = 2;
    private   $baz  = 3;
}

$foo = new Foo();

$reflect = new ReflectionClass($foo);
$props   = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);

foreach ($props as $prop) {
    print $prop->getName() . "\n";
}

var_dump($props);

?>

As you can see also the protected $bar is returned :) And have a look at http://www.php.net/manual/en/reflectionproperty.getvalue.php#98643 there it is shown how to return the value of a protected attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文