Solved

count if in php arrays

Posted on 2014-03-10
3
1,341 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 108

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now