Jump to content

Another question (somewhat solved but lookng for one liner)


sspoke

Recommended Posts

I'm trying to count number of elements that match the element I'm looking for this is for a quantity update system in my shopping cart

 

best i found so far is

 

array array_count_values ( array $input )

 

What I have so far

 

$id = 4
$newqty = 20;
$curqty = 0;

if(array_key_exists($id, $_SESSION['plvlcart'])) {
   $whatToUpdate = $_SESSION['plvlcart'][$id];

   $totalofItem = ???array_count_values???($_SESSION['plvlcart'], $whatToUpdate)
  //blah blah get total amount of elements that match $whatToUpdate array.
  //Do match if the total amount of elements [$curqty] exceeds the $newqty subtract $curqty from $newqty
  //now get a total of elements to remove that match $whatToUpdate
  //now remove a total of X $whatToUpdates (loop+counter)
  //Else if the total amount of elements [$curqty] is less then the $newqty 
  //then insert X amount of $whatToUpdate's into session. (loop)
}

 

I'm missing a few functions for inserting X amount of elements to increase qty to newqty i'd just use a loop okay

same for removing a total of X.. but i'll use another counter here to keep track of amount removed then if it hits that amount exit loop.

 

Any one liners for that? to avoid coding the loops

 

also yah the array_count_values  counts all elements but how do i pick only 1 element to count?

k found

$answer = array_count_values($array);

echo $answer[$whatToUpdate ]

 

nevermind doesn't work haha

 

any array_count_object_values?? or something like that

 

 

here is my test

 


<?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);
//unset($shop[1]);
//$shop = array_merge($shop); //changes
//print_r($shop);
//$shop[] = array("slevel" => 55, "elevel" => 60, "price"=> 10.33);
//print_r($shop);

$quantities = array_count_values($shop);
$test = array( "slevel" => 20, 
                      "elevel" => 60,
                      "price" => 75.32 
                    );

print_r($quantities);
echo $quantities[$test];

 


Warning: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values! in C:\nginx\html\VSKY\test.php on line 28

Warning: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values! in C:\nginx\html\VSKY\test.php on line 28

Warning: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values! in C:\nginx\html\VSKY\test.php on line 28

Warning: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values! in C:\nginx\html\VSKY\test.php on line 28
Array ( ) 
Warning: Illegal offset type in C:\nginx\html\VSKY\test.php on line 35

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.