I want to count specific strings from array values
$array = array("report=D","report=D","report=D","report=I","report=I"); I am going to count Nothe number of "D"D,"U" and "I" And itsU & I, as well as their meanings.
D = Delivered U = Un DeliveredUndelivered I = Invalid I am expecting an output isof:
D = 3 U = 0 I = 2 First, I replace "report=" using str_replace()
, then use array_filter foris used to count specific values.
<?php $array = array( "report=D","report=D","report=D","report=I","report=I" ); $array = str_replace("report=","",$array); function getting_u($n) { return $n == "U"; } function getting_d($n) { return $n == "D"; } function getting_i($n) { return $n == "I"; } $result_of_u = array_filter($array, "getting_u"); $result_of_d = array_filter($array, "getting_d"); $result_of_i = array_filter($array, "getting_i"); print_r($result_of_u); print_r($result_of_d); print_r($result_of_i); echo "Count of U = ".count($result_of_u); echo "\n"; echo "Count of D = ".count($result_of_d); echo "\n"; echo "Count of I = ".count($result_of_i); ?> See a See Here workingworking code here.
My doubt is aboveThe code workingseems to work fine for me, but I don't know whichI'm concerned whether this is the correct way or Not ?/best method of procedure.
I am also unsure if my code contains any strange behaviour or any problem may occurs in future please suggest any good approach with examplepresent/future problems.