Jump to content

Adding to a multi dimension array


djforema

Recommended Posts

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; ?>
Link to comment
https://forums.phpfreaks.com/topic/289208-adding-to-a-multi-dimension-array/
Share on other sites

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; ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.