Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

count if in php arrays

Posted on 2014-03-10
3
1,480 Views
Last Modified: 2014-03-10
Hi,

I have the following arrays:

array1 = ("CONTAR","CONTAR", "NO CONTAR","CONTAR","NO CONTAR","NO CONTAR");
array2 = ("100","100","103","102","100","100");

both arrays have corresponding elements associated: for example the first "CONTAR" in array1 corresponds to 100 in the array2, and so on...

I need to know how many items of 100 are related with "CONTAR", in this case the answer needs to be 2, is like countif in excel. Any idea?
0
Comment
Question by:joyacv2
3 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 250 total points
ID: 39918668
<?php
$array1 = array("CONTAR","CONTAR","NO CONTAR","CONTAR","NO CONTAR","NO CONTAR");
$array2 = array("100","100","103","102","100","100");

$total = 0;
foreach($array1 as $idx => $val)
{
  if($val == "CONTAR")
  {
     $array2_value = $array2[$idx];
     if($array2_value == "100")
     {
        $total++;
     }
  }
}
echo $total . " total that are CONTAR and are also 100";
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39918792
I understand the order of checking this way:
http://www.iconoun.com/demo/temp_joyacv2.php

<?php // temp_joyacv2.php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);

$nums = array( "100",   "100",   "103",      "102",   "100",      "100"       );
$cons = array( "CONTAR","CONTAR","NO CONTAR","CONTAR","NO CONTAR","NO CONTAR" );

$tot = 0;
foreach($nums as $ptr => $val)
{
    if($val == 100)
    {
        if($cons[$ptr] == "CONTAR")
        {
            $tot++;
        }
    }
}
var_dump($tot);

Open in new window

0
 
LVL 1

Author Closing Comment

by:joyacv2
ID: 39919048
both solutions work perfect! Thanks to both!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question