0

I'm pretty new to PHP and programming so I'm having troubles with this thing.

The purpose of the whole situation is to read a column from tab delimited file (already did that), get all different items in it, count them individually and put them in table with

Column1[item value - label], Column2[count].

I have the whole column in 1 dimension array. Now, I want to list all the items there and their counts beside. The problem is, I could have more than 10 different items, even more, so I can't do it manually (name 10 variables and count each) like this:

$arr = array("complete","fail","complete","exit","fail","fail","complete");
function isComplete($value){
    return ($value == "complete") ? true : false;
}

$complete = array_filter($array, 'isComplete');

<tr>
   <td>Complete</td>
   <td><?php echo count($complete)?></td>
</tr>

-- > Complete = 3

I want to avoid manually creating every function for each value because values can differ from file to file.

The number of items in $array can go up to 20+k so I need all automated. Can someone help me with this?

0

3 Answers 3

2

How about going through the array and placing the counts in another array? ($arrCount)

$arr = array("complete","fail","complete","exit","fail","fail","complete");
$arrCount = Array();

foreach($arr as $value){
    $arrCount[$value] = array_key_exists($value,$arrCount) ? $arrCount[$value]+1 : 1;
    //If this key exists, add 1 to it, else make it equal to 1
}

This would create an array with the keys being the various labels and the value equally to the label total.

print_r output:

Array ( [complete] => 3 [fail] => 3 [exit] => 1 )

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

2 Comments

I'm getting "Notice: Undefined index: complete in.." for each $key
Great! Thank you! That's exactly what I wanted to do. I combiened it with @Niels reply with table echo and got the right resultes. Thank you so much!
0

Psuedo code:

$arr = array("complete","fail","complete","exit","fail","fail","complete");
$counts = array();
foreach($arr => $key as $value)
{
    if(array_key_exists($value, $counts))
    {
        $counts[$value]++;
    }else{
        $counts[$value] = 1;
    }
}
foreach($counts => $key as $value)
{
    echo '<tr>
   <td>' . $key . '</td>
   <td>' . $value . '</td>
</tr>';
}

We are making arrays of the elements in the list. If the item exists, increase otherwise create with value 1. Than loop another time to show to the user the total count of each item.

2 Comments

Is this a right way of writing it "foreach($counts => $key as $value)"? I'm getting Syntax error.
Thank you, I used your second part of code with top answer and it worked the way I wanted ;)
0

Maybe I'm missing the point, but what is the problem with a normal loop? For example:

function countByValue($array, $value){
    $count  = 0;
    foreach($array as $val){
        if ($val == $value){
            $count++;
        }
    }
    return $count;
}

And call it with

$complete = countByValue($arr, 'complete');

If you want to count them all at once go with F4r-20 answer.

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.