2012-03-28 2 views
5

J'ai tableau résultat comme ceci:tableau de recherche de PHP dans le tableau

Array 
(
    [0] => stdClass Object 
     (
      [id_global_info] => 78 
      [name] => rfhd 
      [body] => dhfdhdf 
      [contact_author] => mirko 
      [date_created] => 2012-03-15 16:11:54 
      [date_expires] => 2012-04-14 16:11:54 
      [email] => 
      [location_id] => 1 
      [category_id] => 26 
      [tag] => fhdhfdhfd 
      [info_type_id] => 4 
      [user_id] => 3 
     ) 

    [1] => stdClass Object 
     (
      [id_global_info] => 79 
      [name] => rfhd 
      [body] => dhfdhdf 
      [contact_author] => mirko 
      [date_created] => 2012-03-15 16:11:56 
      [date_expires] => 2012-04-14 16:11:56 
      [email] => 
      [location_id] => 1 
      [category_id] => 26 
      [tag] => fhdhfdhfd 
      [info_type_id] => 4 
      [user_id] => 3 
     ) 

    [2] => stdClass Object 
     (
      [id_global_info] => 80 
      [name] => rfhd 
      [body] => dhfdhdf 
      [contact_author] => mirko 
      [date_created] => 2012-03-15 16:11:56 
      [date_expires] => 2012-04-14 16:11:56 
      [email] => 
      [location_id] => 1 
      [category_id] => 26 
      [tag] => fhdhfdhfd 
      [info_type_id] => 4 
      [user_id] => 3 
     ) 
. 
. 
. 
) 

Comment puis-je rechercher un tableau multidimensionnel et compter le nombre de résultats (par exemple, je veux rechercher info_type_id avec une valeur de 4)?

+3

double possible de [valeur de comptage de PHP dans le tableau] (http://stackoverflow.com/questions/9909143/php-count-value-in -array). Vous venez de demander ceci en recherchant 'info_type_id == 1' – jprofitt

+0

Vous envoyez des doublons à vos propres questions. Ne fais pas ça. – netcoder

Répondre

5

avec foreach?

function searchMyCoolArray($arrays, $key, $search) { 
    $count = 0; 

    foreach($arrays as $object) { 
     if(is_object($object)) { 
      $object = get_object_vars($object); 
     } 

     if(array_key_exists($key, $object) && $object[$key] == $search) $count++; 
    } 

    return $count; 
} 

echo searchMyCoolArray($input, 'info_type_id', 4); 
+0

Cela fonctionne. Je vous remercie :) – Sasha

1

Vous devriez essayer ceci:

$counter = 0; 
    $yourArray; // this var is your current array 
    foreach($yourArray as $object){ 
      if($object->info_type_id == 4){ 
       $counter++; 
      } 
    } 
10

Utilisation array_filter pour filtrer le tableau:

function test($arr) { 
    return $arr["info_type_id"] == 4; 
} 

echo count(array_filter($yourArray, "test"));