php创建一个multidimensioanal数组

时间:2016-05-31 12:29:24

标签: php arrays

我正在创建一个这样的数组:

$result[$buyid.'_'.$freeid] = array(
    'itemquantity' => $v,
    'buyproduct' => $_POST['buyproduct'][$k],
    'buyproductid' => $buyid, 
    'freequantity' => $_POST['freequantity'][$k],
    'freeproduct' => $_POST['freeproduct'][$k],
    'freeproductid' => $freeid, 
);

哪个输出我的数组:

Array(
[53_53] => Array
    (
        [itemquantity] => 5
        [buyproduct] => 
        [buyproductid] => 53
        [freequantity] => 1
        [freeproduct] => 
        [freeproductid] => 53
    )

[53_38] => Array
    (
        [itemquantity] => 2
        [buyproduct] => 
        [buyproductid] => 53
        [freequantity] => 1
        [freeproduct] => 
        [freeproductid] => 38
    )

[31_31] => Array
    (
        [itemquantity] => 5
        [buyproduct] => 
        [buyproductid] => 31
        [freequantity] => 1
        [freeproduct] => 
        [freeproductid] => 31
    )

我想以稍微不同的方式存储我的数据。我想基于" buyproductid"存储它。因此,如果有多个" buyproductid"它创建了一个子数组。像这样:

Array(
[53] => Array(
     [53] => Array(
        [itemquantity] => 5
        [buyproduct] => 
        [buyproductid] => 53
        [freequantity] => 1
        [freeproduct] => 
        [freeproductid] => 53
    ),
    [38] => Array(
        [itemquantity] => 5
        [buyproduct] => 
        [buyproductid] => 53
        [freequantity] => 1
        [freeproduct] => 
        [freeproductid] => 53
    )

),

[31] => Array(
    [31] => Array(
        [itemquantity] => 5
        [buyproduct] => 
        [buyproductid] => 31
        [freequantity] => 1
        [freeproduct] => 
        [freeproductid] => 31
    )

 )

实现这一目标的最佳方法是什么?

2 个答案:

答案 0 :(得分:2)

您只需稍微改变一下分配语法:

$result[$buyid][$freeid] = array(
    'itemquantity' => $v,
    'buyproduct' => $_POST['buyproduct'][$k],
    'buyproductid' => $buyid, 
    'freequantity' => $_POST['freequantity'][$k],
    'freeproduct' => $_POST['freeproduct'][$k],
    'freeproductid' => $freeid, 
);

答案 1 :(得分:1)

只需将其更改为:

    Timer t=new Timer();
    TimerTask task=new TimerTask() {
        @Override
        public void run() {
          button.setText("");//Example
        }
    };
    t.scheduleAtFixedRate(task,0,3000);//3 seconds