James
James

Reputation: 3273

PHP Arrays - How to use Multiple variables in array

I have a product import code for Magento that assigns inventory (qty) to a warehouse location (stock_id). The information is passed in an array however my working knowledge of arrays isn't that flash so I'm sure I'm not doing this correctly.

The import is currently done like this however I'm sure it's not the most efficient as I'm saving the product twice.

This will assign a qty of 100 to location 1 (stock_id 1), save the product, then assign a qty of 200 to location 2 (stock_id 2) and then save the product again.

 $stocksData = $product->getStocksData();
        if (!$stockData) {
            $stockData = array();
        }
        $stockData['stock_id'] = 1;
        $stockData['qty'] = 100; 
        $stocksData[$stockId] = $stockData;
        $product->setStocksData($stocksData);

        $product->setCreatedAt(strtotime('now'));
        try {
            $product->save();
            echo "Successful";
        }
        catch (Exception $ex) {
            echo 'There was an error :<br/>' .$ex;
        }

     $stocksData = $product->getStocksData();
            if (!$stockData) {
                $stockData = array();
            }
            $stockData['stock_id'] = 2;
            $stockData['qty'] = 200; 
            $stocksData[$stockId] = $stockData;
            $product->setStocksData($stocksData);


$product->setCreatedAt(strtotime('now'));
try {
    $product->save();
    echo "Successful";
}
catch (Exception $ex) {
    echo 'There was an error :<br/>' .$ex;
}

What I'm trying to achieve is to set all of the values in the array and save once as this will take a lot of load off the script.

I've been playing around with stuff like this, however haven't got anywhere and usually end up with errors:

     $stocksData = $product->getStocksData();
               if (!$stockData) {
                   $stockData = array();
               }
               $stockData = array(
[$stockData['stock_id'] = 1] => $stockData['qty'] = 100,
[$stockData['stock_id'] = 2] => $stockData['qty'] = 200
);
               $stocksData[$stockId] = $stockData;
               $product->setStocksData($stocksData);

I'm assuming it's possible to have all of this information in one array but I'm just not sure how.

Upvotes: 0

Views: 303

Answers (1)

Aiias
Aiias

Reputation: 4748

There are a lot of ways to initialize an array in php.

$stocksData = array(
  'key' => 'value',
  'myarr' => array(
    'nested' => 'array',
    1,
  ),
  'id_copy' => $stocksData['id'],
  'qty' => $stocksData['stock_id'] == 1 ? 100 : 200,
);

For a full explanation of array syntax, check out php's Array documentation. Also note my usage of the ternary operator. You can get around using this syntax by saying something like:

if ($stocksData['id'] == 1) {
  $stocksData['qty'] = 100;
}
else {
  $stocksData['qty'] = 200;
}

Edit:

For your specific use case of combining the requests, take a look below:

$stocksData = $product->getStocksData();
$stocksData[1] = array(
  'stock_id' => 1,
  'qty' => 100,
);
$stocksData[2] = array(
  'stock_id' => 2,
  'qty' => 200,
);
$product->setStocksData($stocksData);

Upvotes: 1

Related Questions