0

当 item 等于 5 时,如何为 itemprices 分配新值。

Object (
    [name] => xxxxx
    [phone] => xxxxxx
    [email] => xxxxxxx
    [items] => Array ( [0] => 0 [1] => 4 [2] => 5 ) 
    [itemprices] => Array ( [0] => 1.00 [4] => 1.00 [5] => 1.00 )
    [itemqtys] => Array ( [0] => 1 [4] => 1 [5] => 1 )
}
4

3 回答 3

0

您已经在 itemprices 内部数组中分配了一个键值 5,以在商品编号等于 5 时存储价格。在这种情况下,只需找出数组并使用商品编号作为您的键:p

$newprice=1.50;
$arr['itemprices'][5]=$newprice; //$arr['itemprices'] is the array you want to access, and also another array, & the item inside it you want to access have the key as 5 (items number) :p
于 2013-09-19T09:57:30.710 回答
0

试试下面的代码:

这里 $array 是父数组(对象)

foreach ($array as $key=>$value){
   if($key == "itemprices"){
    // Get inner Array
    $in_arr = $array[$key];
    // Now you can Assign new value
    $newValue = 10;
    $in_arr[5] = $newValue;
   }
}
于 2013-09-19T09:47:18.780 回答
0

你不能使用关联数组吗?喜欢:

$obj = new stdClass();
$obj->name = 'xxxxx';
$obj->email = 'xxxxx';
$obj->phone = 'xxxxx';
$obj->items = array(
    array( 'quantities' => 1,
           'price' => 1.00, ),
    array( 'quantities' => 1,
           'price' => 1.00, ),
    array( 'quantities' => 1,
           'price' => 1.00, )
);

只是一个让您的生活更轻松的想法。

于 2013-09-19T09:47:53.037 回答