-1

私は取引フィールドを持つデータベースを持っています。これは私のウェブサイトに持ち込まれた製品を表示します。持ち込まれた製品を確認できる管理インターフェイスを開発しようとしています。

弦はこんな感じ 37,2:27,1:5,3:94,10:49,15:

これは基本的に、顧客が製品 ID 番号 37 を数量 2 で注文したことを意味します。彼らのトランザクションには、製品 ID 27 と数量 1 が含まれていました。

product_id,ordered_quantity:nextproduct_id,next_orderedquantity.

この情報を表示するには、これを分解する必要があります。私はphp爆発を試みましたが、いくつかの問題があります。したがって、製品をコロン:で分割し、数量と IDを で分割する必要がありcommaます。文字列は、1 つまたは複数の製品にすることができます。

誰にも何か提案がありますか?

4

2 に答える 2

2
$ids = '37,2:27,1:5,3:94,10:49,15';
$products = explode(':', $ids);
$productAndQuantity = array();
foreach ($products as $product) {
    $exploded = explode(',', $product);
    $productAndQuantity[$exploded[0]] = $exploded[1];
}

商品 ID - 数量配列を取得します。

このデータ保存方法は、スケーラブルではなく、エラーが発生しやすくなります。次のフィールドを持つテーブルを使用しないのはなぜですか: userId、productId、quantity?

于 2012-07-14T22:38:45.993 に答える
-1

ここに私が一緒に投げたものがあります-

$str = '37,2:27,1:5,3:94,10:49,15:';

$a = explode(':',$str); // split by colon ":"

$data = array(); 
foreach ($a as $product) {  // iterate over each product
    $item = explode(',',$product); // split product and quanitity
    $data[$item[0]] = array(  // use product_id [0] as array key
      'product_id'=>$item[0],
      'quantity'=>$item[1]
    );
}

// in this example there is a trailing colon - this removes it.    
array_pop($data);

print_r($data);

Array
(
    [37] => Array
        (
            [product_id] => 37
            [quantity] => 2
        )

    [27] => Array
        (
            [product_id] => 27
            [quantity] => 1
        )

    [5] => Array
        (
            [product_id] => 5
            [quantity] => 3
        )
    ...
)
于 2012-07-14T22:41:08.073 に答える