如何从数组中提取元素并在php中创建一个新元素?

时间:2014-09-02 07:20:59

标签: php arrays duplicates

这是我当前数组的结果:

array(10) {
  [0]=>
  array(5) {
    ["sku"]=>
    string(2) "10"
    ["name"]=>
    string(17) "ACID TARTRIC 1KG "
    ["price"]=>
    float(44)
    ["cantitate"]=>
    string(6) "27.000"
    ["id_gest"]=>
    string(1) "1"
  }
  [1]=>
  array(5) {
    ["sku"]=>
    string(2) "11"
    ["name"]=>
    string(12) "FIR ERT 250 "
    ["price"]=>
    float(8)
    ["cantitate"]=>
    string(5) "7.000"
    ["id_gest"]=>
    string(1) "1"
  }
  [2]=>
  array(5) {
    ["sku"]=>
    string(2) "12"
    ["name"]=>
    string(40) "ZDROBITOR DESCIORCHINATOR ENO 15 SMALTO "
    ["price"]=>
    float(1830)
    ["cantitate"]=>
    string(5) "2.000"
    ["id_gest"]=>
    string(1) "1"
  }
  [3]=>
  array(5) {
    ["sku"]=>
    string(2) "12"
    ["name"]=>
    string(40) "ZDROBITOR DESCIORCHINATOR ENO 15 SMALTO "
    ["price"]=>
    float(1830)
    ["cantitate"]=>
    string(5) "5.000"
    ["id_gest"]=>
    string(2) "11"
  }
  [4]=>
  array(5) {
    ["sku"]=>
    string(2) "13"
    ["name"]=>
    string(42) "ZDROBITOR DESCIORCHINATOR ENO 15 SEMIINOX "
    ["price"]=>
    float(2440)
    ["cantitate"]=>
    string(5) "1.000"
    ["id_gest"]=>
    string(2) "11"
  }
  [5]=>
  array(5) {
    ["sku"]=>
    string(2) "17"
    ["name"]=>
    string(31) "DESCIORCHINATOR ARNO 35 SMALTO "
    ["price"]=>
    float(4150)
    ["cantitate"]=>
    string(5) "2.000"
    ["id_gest"]=>
    string(2) "11"
  }
  [6]=>
  array(5) {
    ["sku"]=>
    string(2) "18"
    ["name"]=>
    string(20) "FERMACTIVE SC 500GR "
    ["price"]=>
    float(49)
    ["cantitate"]=>
    string(6) "23.000"
    ["id_gest"]=>
    string(1) "1"
  }
  [7]=>
  array(5) {
    ["sku"]=>
    string(2) "18"
    ["name"]=>
    string(20) "FERMACTIVE SC 500GR "
    ["price"]=>
    float(49)
    ["cantitate"]=>
    string(5) "4.000"
    ["id_gest"]=>
    string(2) "11"
  }
  [8]=>
  array(5) {
    ["sku"]=>
    string(2) "19"
    ["name"]=>
    string(18) "SULFOMAT 80PU 1KG "
    ["price"]=>
    float(7)
    ["cantitate"]=>
    string(6) "72.000"
    ["id_gest"]=>
    string(1) "1"
  }
  [9]=>
  array(5) {
    ["sku"]=>
    string(3) "100"
    ["name"]=>
    string(19) "FERMACTIVE SB 500g "
    ["price"]=>
    float(59)
    ["cantitate"]=>
    string(6) "50.000"
    ["id_gest"]=>
    string(1) "1"
  }
}

正如你所看到的那样,有重复的元素,比如DESCIORCHINATOR ARNO 35 SMALTO,但是有一些重要元素(意思是:数量)它不是,也就是id_gest。所以,我想创建一个没有重复项的数组,但我也希望将重复项的数量和id_gest相加。 这是我的尝试:

for ($i=0; $i < count($result); $i++) { 
                for ($j = $i+1; $j < count($result)+1; $j++) { 
                    if ($result[$i]['sku'] == $result[$j]['sku']){
                        $qty = $result[$i]['cantitate'] + $result[$j]['cantitate'];
                        //echo $qty."<br>";
                        $id_gest = $result[$i]['id_gest']."%%". $result[$j]['id_gest'];
                        $list[$i] = $result[$i]['sku']."%%".$result[$i]['name']."%%".$result[$i]['price']."%%".$qty."%%".$id_gest;
                        //unset($result[$i]);
                    }else{
                        $qty = $result[$i]['cantitate'];
                        $id_gest = $result[$i]['id_gest']."%%";
                        $list[$i+1] = $result[$i]['sku']."%%".$result[$i]['name']."%%".$result[$i]['price']."%%".$qty."%%".$id_gest;
                    }
                }
            }

$ list是新数组。我做错了什么?

3 个答案:

答案 0 :(得分:0)

结构真的很重要吗?否则,你可以试试这个:

$list = array();
for ($i=0; $i < count($result); $i++) { 
   $sku = $result[$i]['sku']; 
   $qty = @$list[$sku]['qty'] + $result[$i]['cantitate'];
   $id_gest = @$list[$sku]['id_gest'] . '%%' . $result[$i]['id_gest'];

   $list[$sku]['qty'] = $qty;
   $list[$sku]['id_gest'] = $id_gest;
   $list[$sku]['whatever'] =  $sku."%%".$result[$i]['name']."%%".$result[$i]['price']."%%".$qty."%%".$id_gest;
}

答案 1 :(得分:0)

只需一个简单的foreach即可。使用项目的sku并将其作为键推入阵列内部。例如:

$list = array();
foreach($result as $item) {
    $sku = $item['sku'];

    if(!isset($list[$sku])) {
        // if it does not yet exist, push the whole array
        $list[$sku] = $item;
    } else {
        // if it exists already, just add the quantity
        $list[$sku]['cantitate'] += $item['cantitate'];
    }
}

// simple reindex
$list = array_values($list);

答案 2 :(得分:0)

很难理解你正在做的循环,这就是我写另一个循环的原因。如果n#39; sku&#39; key是某种ID而不是

$noDuplicateArray = array();
foreach ($result as $item) {
    if (isset($noDuplicateArray[$item['sku']])) {
        $noDuplicateArray[$item['sku']]['cantitate'] += $item['cantitate'];
    } else {
        $noDuplicateArray[$item['sku']] = $item;
    }
}