2017-06-17 5 views
0

J'ai déjà essayé la recherche, mais mon problème n'a pas été résolu, car mon cas semble être un peu différent.Nombre de valeurs non-vides dans le tableau (php)

Ceci est mon tableau:

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) } 

Certaines entrées ont aucune valeur pour « nom » - donc je veux seulement compter ceux qui ont un nom.

Mais "array_filter" ne vous aide pas au moins comment j'essayé de l'utiliser :(=> array_filter (myarray $);

Répondre

3

Vous pouvez utiliser array_filter sur le nom colume comme celui-ci, live demo

echo count(array_filter(array_column($array, 'name'))); 
+1

Cela a fonctionné pour moi! Super merci! –

+1

J'ai supprimé ma réponse par honte après avoir vu ceci: P C'est une approche utile à connaître. – WizardCoder

0

Vous pouvez utiliser array_filter() comme ci-dessous:

<?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; 
+0

Malheureusement, il n'y a pas ouput je besoin d'un comte, comme « 5 » -... s'il y a 5 entrées avec un nom –

+0

J'ai mis à jour ma réponse. Vous pouvez utiliser 'count ($ array_non_empty)' pour compter les valeurs non vides. @MrKayodoubleu –