0

I have an multi-dimensional array. I want to get count of specific values in all arrays. This arrays in the main array depend on how many comments that I have added.

Here is my array:

array(3) {
  [0]=>
  array(11) { 
    ["comment_id"]=> string(1) "1" 
    ["user_Id"]=> string(1) "2" 
    ["comment"]=> string(18) "Commented! edited!" 
    ["comment_datetime"]=> string(19) "2015-02-20 04:24:28" 
    ["update_at"]=> string(19) "2015-02-20 04:23:18" 
    ["user"]=> string(18) "Nishan Weerasinghe" 
    ["delete"]=> string(1) "0" 
    ["username"]=> string(6) "Nishan" 
    ["picture_id"]=> NULL 
    ["picture"]=> NULL 
    ["picture_ext"]=> NULL 
  } 

  [1]=> 
  array(11) { 
    ["comment_id"]=> string(2) "48" 
    ["user_Id"]=> string(1) "2" 
    ["comment"]=> string(4) "here" 
    ["comment_datetime"]=> string(19) "2015-02-23 12:58:00" 
    ["update_at"]=> string(0) "" 
    ["user"]=> string(18) "Nishan Weerasinghe" 
    ["delete"]=> string(1) "0" 
    ["username"]=> string(6) "Nishan" 
    ["picture_id"]=> NULL 
    ["picture"]=> NULL 
    ["picture_ext"]=> NULL 
  } 
  [2]=> 
  array(11) { 
    ["comment_id"]=> string(2) "49" 
    ["user_Id"]=> string(1) "2" 
    ["comment"]=> string(3) "dfg" 
    ["comment_datetime"]=> string(19) "2015-02-23 14:46:56" 
    ["update_at"]=> string(0) "" 
    ["user"]=> string(18) "Nishan Weerasinghe" 
    ["delete"]=> string(1) "1" 
    ["username"]=> string(6) "Nishan" 
    ["picture_id"]=> NULL 
    ["picture"]=> NULL 
    ["picture_ext"]=> NULL 
  }
}

I want to get a count of how many ["delete"]=> 0 (answer should be 2 according to this ex.) are there in the main array.

1
  • where are your attempts? count + array_filter or good ol' foreach should suffice Commented Feb 24, 2015 at 3:31

4 Answers 4

2
$counter = 0;
foreach($results as $result){
    if($result['delete'] == "0"){
        $counter++;
    }
}
echo $counter;

Like the comment said a simple foreach and some math.

Sign up to request clarification or add additional context in comments.

Comments

0

just use foreach

$count=0;  
foreach($yourarray as $a){

  if($a['delete']=='1')
      $count++;

}

Comments

0

You can do it with functions like this (Need PHP > 5.5.0):

echo array_count_values(array_column($array_string, 'delete'))[0];

Comments

0

Just apply foreach loop as follows:

$count = 0;
foreach($yourarray as $counter){
    if($counter['delete'] == "0"){
        $count++;
    }
}
echo $count;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.