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

以下のコードを試してください:

ここで $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 に答える
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 に答える