0

我有一个数组。print_r 这个数组的结果是这样的:

Array
(
    [flat_rate] => WC_Shipping_Rate Object
        (
            [id] => flat_rate
            [label] => book
            [cost] => 8000
            [taxes] => Array
                (
                )

            [method_id] => flat_rate
        )

    [local_delivery] => WC_Shipping_Rate Object
        (
            [id] => local_delivery
            [label] => dvd
            [cost] => 15000
            [taxes] => Array
                (
                )

            [method_id] => local_delivery
        )

)

我需要像这样创建数组。我尝试通过以下代码访问此数组:

Array("flat_rate" => (object) Array("id" => "flat_rate" ,"label" => "book" ,"cost" => 8000 ,"taxes" => Array ( ) ,"method_id" => "flat_rate") ,"local_delivery" => (object) Array("id" => "local_delivery" ,"label" => "book" ,"cost" => 15000 ,"taxes" => Array ( ) ,"method_id" => "local_delivery"));

但是当 print_r 这个数组时,WC_Shipping_RatestdClass改变

4

1 回答 1

2

很难理解,你真正想做什么。据我了解,您想在里面创建一个包含实例 WC_Shipping_Rate 对象的数组?如果是这样,您必须创建一个 WC_Shipping_Rate 类的新实例,$object = new WC_Shipping_Rate;然后,您可以将其填充并应用于您的数组:

$object = new WC_Shipping_Rate();
$object->id = 'local_delivery'; // OR with setter, if the class has it $object->setId('local_delivery');
$object->label = 'dvd';
// ...etc
$array = array(
    "flat_rate" => $object
);
于 2013-07-05T15:55:02.480 回答