如何动态重命名我的数组键,以便该键可以用作表中的主键?

I'm creating the array dynamically using this tool.

http://174.121.67.116/~achadire/aware5/product_options.php?cid=6

The array output looks like this.

$data = array(
  0 => array(
    0 => 300,
    3 => 186,
  ),
  1 => array(
    0 => 341,
    3 => 186,
  ),
  2 => array(
    0 => 257,
    3 => 186,
  ),
  3 => array(
    0 => 300,
    3 => 360,
  ),
  4 => array(
    0 => 300,
    3 => 187,
  ),
  5 => array(
    0 => 341,
    3 => 360,
  ),
  6 => array(
    0 => 341,
    3 => 187,
  ),
  7 => array(
    0 => 257,
    3 => 360,
  ),
  8 => array(
    0 => 257,
    3 => 187,
  ),
);

0 => Array is the product. [0]=>300, [2]=>186 represent a unique product configuration for the given array key. (i.e blue, big.) (each vale is a id for an attribute.)

So I want to upload the results to products table in MySQL. Here is my problem:

First, how do I make my array key a unique 7 digit product code. In other words, how to I reindex my array with a unique 7 digit code so that I never use the same key for an array twice. I assume this means figuring out the next value in the mysql database and then looping through the array with i++ before pushing to the table.

Second, the same configuration should only be applied to one product. So if the array key combination already exists it should skip to the next array.

In short how do I turn my array key into a unique 7 digit product code? Also, when I upload my array how to I skip over values combinations that already exists in the database so I don't have two different product codes with the same configuration.

What I ended up doing was inserting a record into my skus table and returning the primary key to an array. The I reidex my my array with the new key values and load it into my skus_options table. Here is the code. Hope it helps someone down the road.

// inserts records into skus table and creates an array of skuids.
for ($i = 0; $i < $count; $i++) {
$sql = "INSERT INTO skus (idcodes, dateadded) VALUES ('$cid', '$dateadded')";
$result = mysql_query($sql, $dbc) or die(mysql_error());
$sid = mysql_insert_id();
array_push($skuids, $sid);
}
// creates an array using skuid as the array key and inserts the array into the          skus_options table.
$skus = array_combine($skuids, array_values($skus));
foreach ($skus as $key => $options) {
foreach ($options as $option => $value) {
$query = "INSERT INTO skus_options (skus_options.idskus, skus_options.valueid,   skus_options.idcodes, skus_options.dateadded) VALUES ('$key', '$value', '$cid','$dateadded')";
     $result = mysql_query($query, $dbc) or die(mysql_error());
  }
}
  1. Stop figuring out how to generate unique array ids by fetching the next id in records sequence. You will have big troubles with concurrency. Change your ID field to be an autoincrement and let the DBMS handle that.

  2. Create a UNIQUE constraint on the color and size attributes of the products table. Then, when you insert use an INSERT ... ON DUPLICATE UPDATE syntax, so you will never have a product with the same attributes.