如何输出值?

时间:2012-08-16 03:06:02

标签: php

$i = 1;
 foreach ($product->getOptions() as $o) {
     $values = $o->getValues();
     foreach ($values as $v) {
            print_r($v->getData());
     }
     $i++;
 }

以上代码输出以下结果:

Array
(
    [option_type_id] => 9
    [option_id] => 3
    [sku] => 
    [sort_order] => 0
    [default_title] => Black
    [store_title] => 
    [title] => Black
    [default_price] => 0.0000
    [default_price_type] => fixed
    [store_price] => 
    [store_price_type] => 
    [price] => 0.0000
    [price_type] => fixed
)
Array
(
    [option_type_id] => 7
    [option_id] => 3
    [sku] => 
    [sort_order] => 0
    [default_title] => Red
    [store_title] => 
    [title] => Red
    [default_price] => 0.0000
    [default_price_type] => fixed
    [store_price] => 
    [store_price_type] => 
    [price] => 0.0000
    [price_type] => fixed
)
Array
(
    [option_type_id] => 8
    [option_id] => 3
    [sku] => 
    [sort_order] => 0
    [default_title] => White
    [store_title] => 
    [title] => White
    [default_price] => 0.0000
    [default_price_type] => fixed
    [store_price] => 
    [store_price_type] => 
    [price] => 0.0000
    [price_type] => fixed
)

我想输出[title]值。我怎么做?谢谢。我曾尝试使用$v->getData()['title'],但它没有用。

1 个答案:

答案 0 :(得分:4)

在php5.4之前,你不能做$v->getData()['title'],你需要使用一个变量。

$i = 1;
foreach ($product->getOptions() as $o) {
    $values = $o->getValues();
    foreach ($values as $v) {
        $data = $v->getData();
        echo $data['title'];
    }
    $i++;
}