doumixiang2227 2011-10-26 11:37
浏览 37
已采纳

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

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.

  • 写回答

2条回答 默认 最新

  • duanmie9682 2011-10-29 17:27
    关注

    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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度