Nieuw lid |
|
Was leukerdje om uit te zoeken!, korter kon ik het zo snel niet krijgen, maar het lijkt wel goed te werken hier
<?php
$producten = array(
array("product" => "nietmachine", "aantal" => 1),
array("product" => "schaar", "aantal" => 2),
array("product" => "pen", "aantal" => 3),
array("product" => "nietmachine", "aantal" => 11),
array("product" => "schaar", "aantal" => 22),
array("product" => "pen", "aantal" => 33),
);
$new_array = array();
$i = 0;
foreach($producten as $key)
{
if(!in_array($producten[$i]['product'], $new_array))
{
$new_array[] = $producten[$i]['product'];
$new_array_totals[] = $producten[$i]['aantal'];
}
else
{
$new_array_totals[array_search($producten[$i]['product'], $new_array)] += $producten[$i]['aantal'];
}
$i++;
}
$final = array_combine ($new_array, $new_array_totals);
?>
<pre>
<?php print_r($final);?>
</pre>
<?php
/*
Array
(
[nietmachine] => 12
[schaar] => 24
[pen] => 36
)
?>
<?php array("product" => "nietmachine", "aantal" => 1), array("product" => "schaar", "aantal" => 2), array("product" => "pen", "aantal" => 3), array("product" => "nietmachine", "aantal" => 11), array("product" => "schaar", "aantal" => 22), array("product" => "pen", "aantal" => 33), ); $i = 0; foreach($producten as $key) { if(!in_array($producten[$i]['product'], $new_array)) { $new_array[] = $producten[$i]['product']; $new_array_totals[] = $producten[$i]['aantal']; } else { $new_array_totals[array_search($producten[$i]['product'], $new_array)] += $producten[$i]['aantal']; } $i++; } $final = array_combine ($new_array, $new_array_totals); ?> <pre> </pre> <?php /* Array ( [nietmachine] => 12 [schaar] => 24 [pen] => 36 ) ?>
edit, print_r vergeten
|