0

我正在使用它来创建一个数组:

foreach($results as $tire){
            $group_price[] = array($tire->group, $tire->price);
        }

我的结果是:

Array ( 
    [0] => Array (
        [0] => MAXAT 
        [1] => 118.91 
        ) 
    [1] => Array (
        [0] => FZSUV 
        [1] => 137.81 
    ) 
    [2] => Array ( 
        [0] => MAXAT 
        [1] => 153.79 
    )
)

我希望我的结果是:

Array (
    [0] => Array (
        [group] => MAXAT 
        [price] => 118.91
    )
    [1] => Array (
        [group] => FZSUV 
        [price] => 137.81
    )
    [2] => Array (
        [group] => MAXAT 
        [price] => 153.79
    )
)

我只是不确定如何改变我的 foreach 以改变输出。

4

5 回答 5

2

像这样:

foreach($results as $tire){
    $group_price[] = array(
        'group' => $tire->group,
        'price' => $tire->price
    );
}
于 2013-10-21T19:55:18.907 回答
0

尝试:

foreach($results as $tire){
            $group_price[] = array('group' => $tire->group,'price' => $tire->price);
        }
于 2013-10-21T19:55:18.463 回答
0

就像这样:

foreach($results as $tire){
     $group_price[] = array(
                            'group' => $tire->group,
                            'price' => $tire->price
                           );
}

为您的数组元素添加键,仅此而已。

于 2013-10-21T19:55:28.600 回答
0
foreach($results as $tire){
            $group_price[] = array('group'=>$tire->group, 'price'=>$tire->price);
        }
于 2013-10-21T19:55:39.150 回答
0

尝试

array('group' => $tire->group, 'price' => $tire->price);
于 2013-10-21T19:56:46.723 回答