0

I tried the search already, but my problem was not solved there, since my case seems to be a bit different.

This is my array:

array(14) { [0]=> array(4) { ["index"]=> int(1) ["name"]=> string(19) "Parge Lenis die ..." ["score"]=> int(0) ["time"]=> string(8) "05:51:23" } [1]=> array(4) { ["index"]=> int(2) ["name"]=> string(8) "jiengels" ["score"]=> int(0) ["time"]=> string(8) "05:51:18" } [2]=> array(4) { ["index"]=> int(3) ["name"]=> string(6) "n!n-oX" ["score"]=> int(0) ["time"]=> string(8) "05:47:27" } [3]=> array(4) { ["index"]=> int(4) ["name"]=> string(8) "Maraskan" ["score"]=> int(0) ["time"]=> string(8) "05:44:45" } [4]=> array(4) { ["index"]=> int(5) ["name"]=> string(0) "" ["score"]=> int(0) ["time"]=> string(8) "05:42:56" } [5]=> array(4) { ["index"]=> int(6) ["name"]=> string(0) "" ["score"]=> int(0) ["time"]=> string(8) "04:32:21" } [6]=> array(4) { ["index"]=> int(7) ["name"]=> string(5) "Heavy" ["score"]=> int(0) ["time"]=> string(8) "03:29:39" } [7]=> array(4) { ["index"]=> int(8) ["name"]=> string(0) "" ["score"]=> int(0) ["time"]=> string(8) "02:59:28" } [8]=> array(4) { ["index"]=> int(9) ["name"]=> string(0) "" ["score"]=> int(0) ["time"]=> string(8) "02:54:37" } [9]=> array(4) { ["index"]=> int(10) ["name"]=> string(0) "" ["score"]=> int(0) ["time"]=> string(8) "02:53:33" } [10]=> array(4) { ["index"]=> int(11) ["name"]=> string(6) "Fischi" ["score"]=> int(0) ["time"]=> string(8) "02:12:36" } [11]=> array(4) { ["index"]=> int(12) ["name"]=> string(0) "" ["score"]=> int(0) ["time"]=> string(8) "02:02:50" } [12]=> array(4) { ["index"]=> int(13) ["name"]=> string(0) "" ["score"]=> int(0) ["time"]=> string(8) "01:09:53" } ["count"]=> int(13) }

Some of the entries have no value for "name" - therefore I only want to count those, which have a name.

But "array_filter" does not help at least how I tried to use it :( => array_filter($myarray);

2 Answers 2

3

You can use array_filter on the name colume like this, live demo.

echo count(array_filter(array_column($array, 'name')));
Sign up to request clarification or add additional context in comments.

1 Comment

I deleted my answer out of shame after seeing this :P This is a helpful approach to know.
0

You can use array_filter() like below:

<?php

$array = array(
            array("index" => 1, "name" => "Parge Lenis die ..." , "score" => 0, "time" => "05:51:23"),
            array("index" => 2, "name" => "jiengels" , "score" => 0, "time" => "05:51:18"),
            array("index" => 3, "name" => "n!n-oX" , "score" => 0, "time" => "05:47:27"),
            array("index" => 4, "name" => "Maraskan" , "score" => 0, "time" => "05:44:45"),
            array("index" => 5, "name" => "" , "score" => 0, "time" => "05:42:56"),
            array("index" => 6, "name" => "" , "score" => 0, "time" => "04:32:21"),
            array("index" => 7, "name" => "Heavy" , "score" => 0, "time" => "03:29:39"),
        ); 

$array_non_empty = array_filter($array, function($item){
    return (isset($item['name']) && $item['name']) ? TRUE : FALSE;
});

$count_non_empty = count($array_non_empty); // number of non-empty values

echo $count_non_empty;

2 Comments

Unfortunatly there is not any ouput. I would need a count, like "5" - if there are 5 entries with a name.
I updated my answer. You can use count($array_non_empty) to count non-empty values. @MrKayodoubleu

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.