为对象分配值#?

时间:2011-10-25 11:58:44

标签: php object

我有一个

stdClass Object ( [cart_item_id] => 319299 
[cart_id] => 1 
[nid] => 2140 
[qty] => 15 
[changed] => 1319485739 
[data] => Array ( 
     [gift_description] => ghgfdhfghfh 
     [gift_email] => hgffghhg@gjfdhjk.com
     [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] => hgffghhg@gjfdhjk.com
     [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] => hgffghhg@gjfdhjk.com
     [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] => hgffghhg@gjfdhjk.com
     [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)并取$ item-> data ['price']并将其分配给$ item [$ X] - >价格

但我不知道如何在PHP中这样做。有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

如果您有一个包含$item个对象的数组,请尝试使用:

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

编辑:它适用于对象的数组。如果你只使用单个对象 - 省略foreach并只使用:

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

答案 1 :(得分: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'],'$');
}