sspoke Posted June 2, 2010 Share Posted June 2, 2010 I finally wrote this due to the lack of php functions to count existing array objects inside a master array. Can this be optimized? <?php $shop = array( array("slevel"=>1, "elevel" => 50, "price" => 15.75 ), array( "slevel" => 20, "elevel" => 60, "price" => 75.32 ), array( "slevel" => 3, "elevel" => 60, "price" => 25.50, ), array( "slevel" => 20, "elevel" => 60, "price" => 75.32 ) ); print_r($shop); $test = array( "slevel" => 20, "elevel" => 60, "price" => 75.32 ); $prevQty = 0; $newQty = 20; $totalitems = count($shop); print "Shop total: " . $totalitems; for($i=0;$i < $totalitems; $i++) { /*foreach($shop as $item) { if($item['slevel'] == $test['slevel'] && $item['elevel'] == $test['elevel'] && $item['price'] == $test['price']) {*/ if($shop[$i]['slevel'] == $test['slevel'] && $shop[$i]['elevel'] == $test['elevel'] && $shop[$i]['price'] == $test['price']) { $prevQty++; if($prevQty > $newQty) { print "prevQty: " . $prevQty . " newQty" . $newQty; unset($shop[$i]); } } } print_r($shop); $shop = array_merge($shop); //fix unsetted sequence if($prevQty < $newQty) { $total=$newQty-$prevQty; for($i=1;$i<=$total;$i++) { $shop[] = $test; } } print_r($shop); Link to comment https://forums.phpfreaks.com/topic/203664-any-way-to-optimize-this/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.