2011-10-25 236 views
0

我有一個將值賦給對象#?

stdClass Object ([cart_item_id] => 319299 
[cart_id] => 1 
[nid] => 2140 
[qty] => 15 
[changed] => 1319485739 
[data] => Array ( 
    [gift_description] => ghgfdhfghfh 
    [gift_email] => [email protected] 
    [gift_sender] => gfdhgfdhgfdhgf 
    [gift_sendDate] => 2011-10-24 
    [gift_title] => gfdgfdgdf 
    [gift_card] => 2130 
    [gift_price] => $26 
    [module] => uc_product 
) 
[title] => Gift Card 
[vid] => 2140 
[cost] => 0 
[price] => 1 
[weight] => 0 
[module] => uc_product 
[model] => giftcard 
) stdClass Object ([cart_item_id] => 319299 
[cart_id] => 1 
[nid] => 2140 
[qty] => 15 
[changed] => 1319485739 
[data] => Array ( 
    [gift_description] => ghgfdhfghfh 
    [gift_email] => [email protected] 
    [gift_sender] => gfdhgfdhgfdhgf 
    [gift_sendDate] => 2011-10-24 
    [gift_title] => gfdgfdgdf 
    [gift_card] => 2130 
    [gift_price] => $26 
    [module] => uc_product 
) 
[title] => Gift Card 
[vid] => 2140 
[cost] => 0 
[price] => 1 
[weight] => 0 
[module] => uc_product 
[model] => giftcard 
) ... etc 

現在我想要做的就是通過這些組中的每一個循環(的foreach爲例),並採取了價格數據和標題,並將其分配給價格和標題。所以我會結束。

stdClass Object ([cart_item_id] => 319299 
[cart_id] => 1 
[nid] => 2140 
[qty] => 15 
[changed] => 1319485739 
[data] => Array ( 
    [gift_description] => ghgfdhfghfh 
    [gift_email] => [email protected] 
    [gift_sender] => gfdhgfdhgfdhgf 
    [gift_sendDate] => 2011-10-24 
    [gift_title] => gfdgfdgdf 
    [gift_card] => 2130 
    [gift_price] => $26 
    [module] => uc_product 
) 
[title] => Gift Card 
[vid] => 2140 
[cost] => 0 
[price] => 1 
[weight] => 0 
[module] => uc_product 
[model] => giftcard 
) stdClass Object ([cart_item_id] => 319299 
[cart_id] => 1 
[nid] => 2140 
[qty] => 15 
[changed] => 1319485739 
[data] => Array ( 
    [gift_description] => ghgfdhfghfh 
    [gift_email] => [email protected] 
    [gift_sender] => gfdhgfdhgfdhgf 
    [gift_sendDate] => 2011-10-24 
    [gift_title] => gfdgfdgdf 
    [gift_card] => 2130 
    [gift_price] => $26 
    [module] => uc_product 
) 
[title] => Gift Card 
[vid] => 2140 
[cost] => 0 
[price] => 1 
[weight] => 0 
[module] => uc_product 
[model] => giftcard 
) ... etc 

但我想這樣做,每個,現在在一個正常的陣列我會通過每個組(在這種情況下$項目),環路,並採取$用品 - >數據[「價格」]並將其分配到$ item [$ x] - >價格

但我不知道如何在PHP中做到這一點。任何幫助?

回答

0

循環從哪裏來?你談論的是foreach,但是你還沒有提供一個需要迭代的例子。

可以「複製」這樣的價值觀:

$object->title = $object->data['gift_title']; 
$object->price = (float) ltrim($object->data['gift_price'],'$'); 

...如果您有過要循環這些對象的數組,你可以這樣做:

foreach ($arrayOfObjects as &$object) { 
    // Note the use of & to get a reference and make sure we modify 
    // the actual object, not a copy of it 
    $object->title = $object->data['gift_title']; 
    $object->price = (float) ltrim($object->data['gift_price'],'$'); 
} 
+0

這會一次返回一個值。因此,對象成爲標題,然後是vid等。而不是單個對象,每個對象都是可訪問的對象。 – Steven

+0

查看第一個代碼,而不是第二個。 – DaveRandom

0

如果你有$item對象的數組,嘗試用:

foreach ($items as &$item) { 
    $item->title = $item->data['gift_title']; 
    $item->price = (float) str_replace('$', '', $item->data['gift_price']); 
} 

編輯:它爲對象的陣列。如果您僅使用單個對象工作 - ommit foreach並僅使用:

$item->title = $item->data['gift_title']; 
    $item->price = (float) str_replace('$', '', $item->data['gift_price']); 
+0

由於與以下相同的原因不起作用。 – Steven

+0

@Steven看看我的編輯。 – hsz