Tony Stark Tony Stark - 4 months ago 8
PHP Question

PHP - Trying to create an array seems to not work

This is the code:

function create_order_with_custom_products()
{
$orderGenerator = new OrderGenerator();
$orderGenerator->setCustomer(6907);

$orderGenerator->createOrder(array(
// Add configurable product
array(
'product' => 30151,
'qty' => 1
), array(
'product' => 30150,
'qty' => 2
),
));
}


I have to create array with such structure:

array(
'product' => 30151,
'qty' => 1
), array(
'product' => 30150,
'qty' => 2
),


I am trying to create an array with same structure like that:

foreach($ItemsInCart['productid'] as $key=>$value){
$ProductId = $value;
$ProductQty = $ItemsInCart["productqty"][$key];
$product_id = $ProductId; // Replace id with your product id
$qty = $ProductQty; // Replace qty with your qty

$ItemsId[] = ['product' => $ProductId, 'qty' => $ProductQty];
}


This gives me result of:


Array ( [0] => Array ( [product] => 30143 [qty] => 1 ) [1] => Array (
[product] => 30144 [qty] => 2 ) [2] => Array ( [product] => 30145
[qty] => 3 ) [3] => Array ( [product] => 30146 [qty] => 4 ) [4] =>
Array ( [product] => 30147 [qty] => 5 ) )


All i want to know is why this :

function create_order_with_custom_products()
{
$orderGenerator = new OrderGenerator();
$orderGenerator->setCustomer(6907);

$orderGenerator->createOrder(array(
// Add configurable product
array(
'product' => 30151,
'qty' => 1
), array(
'product' => 30150,
'qty' => 2
),
));
}


Is not the same as this:

function create_order_with_custom_products()
{
$orderGenerator = new OrderGenerator();
$orderGenerator->setCustomer(6907);

$orderGenerator->createOrder(array($ItemsId));
}


Why the second approach is not working, where is my mistake?

Answer

If your

$ItemsId = []; //declaration
$ItemsId [] = ["product"=>30151,"qty"=>1]; //pusing an array with key 'product' & 'qty' to $ItemsId

UPDATE

This line

$orderGenerator->createOrder(array($ItemsId));

is not working because $ItemsId is already an array and you are putting this inside a new array with this array($ItemsId). This will look like

Array([0] => 
     Array ([0] =>  
            Array(
                 [0] =>Array  ( [product] => 30143,[qty] => 1 ) 
                 [1] => Array ( [product] => 30144,[qty] => 2 ) 
                 [2] => Array ( [product] => 30145,[qty] => 3 ) 
                 [3] => Array ( [product] => 30146,[qty] => 4 ) 
                 [3] => Array ( [product] => 30147,[qty] => 5 ) 
            )
      )
)

But the expected array should look like

      Array ([0] =>  
             Array(
                   [0] =>Array  ( [product] => 30143,[qty] => 1 ) 
                   [1] => Array ( [product] => 30144,[qty] => 2 ) 
                   [2] => Array ( [product] => 30145,[qty] => 3 ) 
                   [3] => Array ( [product] => 30146,[qty] => 4 ) 
                   [3] => Array ( [product] => 30147,[qty] => 5 ) 
             )
       )

To overcome this issue just change

$orderGenerator->createOrder(array($ItemsId)); 

To

$orderGenerator->createOrder($ItemsId);