djforema Posted June 19, 2014 Share Posted June 19, 2014 (edited) I'm trying to add a value to my multi dimension array. I want to add a 'quantity' value to the inner array. This is what I have now: Array ( [0] => stdClass Object ( [0] => stdClass Object ( [name] => BRACKET [option_sku] => [value] => B-Line BB7-16 ) [1] => stdClass Object ( [name] => BOX [option_sku] => [value] => Crouse Hinds TP40DPF ) [2] => stdClass Object ( [name] => Mud Ring[option_sku] => [value] => Rise Single Gang ) [3] => stdClass Object ( [name] => MC Cable[option_sku] => [value] =>black,white,green)) [1] => stdClass Object ( [0] => stdClass Object ( [name] => BRACKET [option_sku] => [value] => B-Line BB7-16 ) [1] => stdClass Object ( [name] => BOX [option_sku] => [value] => Crouse Hinds TP40DPF ) [2] => stdClass Object ( [name] => Mud Ring[option_sku] => [value] => Rise Single Gang ) [3] => stdClass Object ( [name] => MC Cable[option_sku] => [value] =>black,white,green)) ) I'm trying to make it like this: Array ( [0] => stdClass Object ( [0] => stdClass Object ( [name] => BRACKET [option_sku] => [value] => B-Line BB7-16 [quantity] => 5 ) [1] => stdClass Object ( [name] => BOX [option_sku] => [value] => Crouse Hinds TP40DPF [quantity] => 5) [2] => stdClass Object ( [name] => Mud Ring[option_sku] => [value] => Rise Single Gang [quantity] => 5) [3] => stdClass Object ( [name] => MC Cable[option_sku] => [value] =>black,white,green [quantity] => 5)) [1] => stdClass Object ( [0] => stdClass Object ( [name] => BRACKET [option_sku] => [value] => B-Line BB7-16 [quantity] => 5) [1] => stdClass Object ( [name] => BOX [option_sku] => [value] => Crouse Hinds TP40DPF [quantity] => 5) [2] => stdClass Object ( [name] => Mud Ring[option_sku] => [value] => Rise Single Gang [quantity] => 5) [3] => stdClass Object ( [name] => MC Cable[option_sku] => [value] =>black,white,green [quantity] => 5)) ) I'm trying something like this but it isn't working. <?php //This is the array I want to add to. $product_options = $registry->toObject(); //This is the value of what I want to add to the array. With the key being 'quantity'. $number = $item->orderitem_quantity; ?> <?php foreach($product_options as $key => $quan) ?> <?php $product_options[$key]['quantity'] = $number; ?> <?php endforeach; ?> Edited June 19, 2014 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/289208-adding-to-a-multi-dimension-array/ Share on other sites More sharing options...
Zane Posted June 19, 2014 Share Posted June 19, 2014 A quick and dirty way is to double loop <?php foreach($product_options as $key => $quan) ?> <?php foreach($quan as $k=>$value) { $value[$k]['quantity'] = $number; } $product_options[$key]['quantity'] = $number; ?> <?php endforeach; ?> Quote Link to comment https://forums.phpfreaks.com/topic/289208-adding-to-a-multi-dimension-array/#findComment-1482903 Share on other sites More sharing options...
ginerjm Posted June 19, 2014 Share Posted June 19, 2014 Why so many php tags in your code? Quote Link to comment https://forums.phpfreaks.com/topic/289208-adding-to-a-multi-dimension-array/#findComment-1482904 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.