Skip to main content
improved formatting, grammar
Source Link
Quill
  • 12.1k
  • 5
  • 41
  • 94

count Count specific array values

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.

count specific array values

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 No of "D","U" and "I" And its meanings

D = Delivered U = Un Delivered I = Invalid 

I am expecting output is

D = 3 U = 0 I = 2 

First I replace "report=" using str_replace

  then use array_filter for 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 Here working code

My doubt is above code working fine for me, but I don't know which is the correct way or Not ?

if my code contains any strange behaviour or any problem may occurs in future please suggest any good approach with example

Count specific array values

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 the number of D,U & I, as well as their meanings.

D = Delivered U = Undelivered I = Invalid 

I am expecting an output of:

D = 3 U = 0 I = 2 

First, I replace "report=" using str_replace(), then array_filter is 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 working code here.

The code seems to work fine for me, but I'm concerned whether this is the correct/best method of procedure.

I am also unsure if my code contains any strange behaviour or present/future problems.

Source Link
Prakash
  • 11
  • 1
  • 2

count specific array values

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 No of "D","U" and "I" And its meanings

D = Delivered U = Un Delivered I = Invalid 

I am expecting output is

D = 3 U = 0 I = 2 

First I replace "report=" using str_replace

then use array_filter for 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 Here working code

My doubt is above code working fine for me, but I don't know which is the correct way or Not ?

if my code contains any strange behaviour or any problem may occurs in future please suggest any good approach with example