如何将数组的多个元素提取到新数组中

时间:2013-10-09 20:30:02

标签: php arrays

我的问题是如何使用这个数组:

Array (
    [0] => stdClass Object (
        [item] => 0
        [size] => 2657017
        [group] => MAXAT
        [description] => 265/70R17 MAXTRAC A/T 115T 00K
        [sort4] => 115
        [sort5] => T
        [sort6] =>
        [price] => 118.91
    )
    [1] => stdClass Object (
        [item] => 8127
        [size] => 2657017
        [group] => FZSUV
        [description] => 265/70R17 FUZION SUV OWL 115T 50K
        [sort4] => 115
        [sort5] => T
        [sort6] =>
        [price] => 137.81
    )
    [2] => stdClass Object (
        [item] => 0
        [size] => 2657017
        [group] => MAXAT
        [description] => LT265/70R17 MAXTRAC A/T 118S E 00K
        [sort4] => 118
        [sort5] => S
        [sort6] => E
        [price] => 153.79
    )
    [3] => stdClass Object (
        [item] => 1237
        [size] => 2657017
        [group] => ATS
        [description] => 265/70R17 GEO AT-S OWL 113S 50K
        [sort4] => 113
        [sort5] => S
        [sort6] =>
        [price] => 167.15
    )
)

并将其转换为此数组(不运行其他查询):

Array (
    [0] => stdClass Object (
        [group] => MAXAT
        [price] => 118.91
    )
    [1] => stdClass Object (
        [group] => FZSUV
        [price] => 137.81
    )
    [2] => stdClass Object (
        [group] => MAXAT
        [price] => 153.79
    )
    [3] => stdClass Object (
        [group] => ATS
        [price] => 167.15
    )
)

我想要实现的就是将第一个数组中的组和价格拉到一个新数组中。

2 个答案:

答案 0 :(得分:2)

$newEntries = array();
foreach ($originalEntries as $originalEntry) {
  $newEntry = new stdClass();
  $newEntry->group = $originalEntry->group;
  $newEntry->price = $originalEntry->price;
  $newEntries[] = $newEntry;
}

答案 1 :(得分:0)

你能用foreach遍历数组并将值插入新数组吗?!

$new_array = array();
$i = 0;
foreach($array as $k => $v){
    $new_array[$i]['group'] = $v['group'];
    $new_array[$i]['price'] = $v['price'];
    $i++;
}