2012-03-12 4 views
0

j'ai une syntaxe foreach qui génère des résultats suivants:Comparez différents articles dans des tableaux différents

array 
    0 => 
    array 
     'value' => string '2012-05-09T12:00:00' (length=19) 
     'value2' => string '2012-05-09T15:00:00' (length=19) 
     'timezone' => string 'Europe/Paris' (length=12) 
     'timezone_db' => string 'UTC' (length=3) 
     'date_type' => string 'date' (length=4) 
    1 => 
    array 
     'value' => string '2012-03-14T13:00:00' (length=19) 
     'value2' => string '2012-03-14T16:00:00' (length=19) 
     'timezone' => string 'Europe/Paris' (length=12) 
     'timezone_db' => string 'UTC' (length=3) 
     'date_type' => string 'date' (length=4) 
    2 => 
    array 
     ... 
array 
    0 => 
    array 
     'value' => string '2012-02-08T13:00:00' (length=19) 
     'value2' => string '2012-02-08T16:00:00' (length=19) 
     'timezone' => string 'Europe/Paris' (length=12) 
     'timezone_db' => string 'UTC' (length=3) 
     'date_type' => string 'date' (length=4) 
    1 => 
    array 
     'value' => string '2012-03-14T13:00:00' (length=19) 
     'value2' => string '2012-03-14T16:00:00' (length=19) 
     'timezone' => string 'Europe/Paris' (length=12) 
     'timezone_db' => string 'UTC' (length=3) 
     'date_type' => string 'date' (length=4) 
    2 => 
    array 
     ... 

Les ... représentent beaucoup plus de code, tous avec la même structure.

Et voici le code php:

foreach ($result as $term) { 
    $node = node_load($term->nid); 
    $dates = $node->field_date['und']; 
    var_dump($dates); 
} 

L'objectif est de comparer les résultats et les vérifier toutes les inégalités. Alors, que je vise est un script qui me permet de comparer $dates[0]['value'] du premier tableau et $dates[0]['value'] du deuxième réseau ...

Je pensais à composer de nouveaux tableaux avec tous les 0 éléments ou tous les éléments 1 et plus tard, vérifiez-les, mais jusqu'à présent, pas de chance. Est-ce que quelqu'un a une autre idée?

Répondre

1
$array[] = array(
    'value'  => '2012-03-14T13:00:00', 
    'value2'  => '2012-03-14T16:00:00',  // <--- Error 
    'timezone' => 'Europe/Paris', 
    'timezone_db' => 'UTC', 
    'date_type' => 'date', 
); 
$array[] = array(
    'value'  => '2012-03-14T13:00:00a',  // <--- Error 
    'value2'  => '2012-03-14T16:00:00', 
    'timezone' => 'Europe/Paris', 
    'timezone_db' => 'UTC', 
    'date_type' => 'date', 
); 
$array[] = array(
    'value'  => '2012-03-14T13:00:00', 
    'value2'  => '2012-03-14T16:00:00tt', // <--- Error 
    'timezone' => 'Europe/Paris', 
    'timezone_db' => 'UTC', 
    'date_type' => 'date11', 
); 

$errors = array(); 
for($i=0; $i < count($array)-1; $i++) 
    $errors = array_merge(array_diff_assoc($array[$i], $array[$i+1])); 

var_dump($errors); 

/* Output 
* ------ 
* Array 
* (
*  [value] => 2012-03-14T13:00:00a 
*  [value2] => 2012-03-14T16:00:00 
*  [date_type] => date 
*) 
*/ 
+0

Merci pour la patience et la persévérance pendant le processus de résolution de cette énigme :) – Michiel

2
$array = array(); 

$array[] = array(
    'value'  => '2012-03-14T13:00:00', 
    'value2'  => '2012-03-14T16:00:00', 
    'timezone' => 'Europe/Paris', 
    'timezone_db' => 'UTC', 
    'date_type' => 'date', 
); 
$array[] = array(
    'value'  => '2012-03-14T13:00:00', 
    'value2'  => '2012-03-14T16:00:00dddd',  // <--- Error is here 
    'timezone' => 'Europe/Paris', 
    'timezone_db' => 'UTC', 
    'date_type' => 'date', 
); 
$array[] = array(
    'value'  => '2012-03-14T13:00:00', 
    'value2'  => '2012-03-14T16:00:00', 
    'timezone' => 'Europe/Paris', 
    'timezone_db' => 'UTC', 
    'date_type' => 'date', 
); 

if(count($array) > 1) 
{ 
    $error = false; 
    $keyCount = count($array); 

    for($i=0; $i < $keyCount-1; $i++) 
    { 
     foreach($array[$i] as $key=>$val) 
     { 
      if($array[$i+1][$key] != $val) 
      { 
       $error = $key; 
       break; 
      } 
     } 
    } 
} 

if($error) 
    echo "Error key = " . $error; 
else 
    echo "No errors"; 



Sortie: Error key = value2

Edit: Les changements pour refléter la nouvelle compréhension de la question.

+0

Merci! Mais j'ai un nombre indéfini de tableaux ... Donc, il pourrait y en avoir un, mais leur pourrait être de 5 sur 6 ... – Michiel

+0

Vous devrez avoir une boucle foreach imbriquée, j'ai raté ce bit. Mais il serait plus efficace de créer un nouveau tableau, puis de l'exécuter de toute façon. – Bradmage

+0

Oh, c'est vrai. Oui, ça le rend un peu plus compliqué. – Bradmage

0

Je pense que c'est une façon

$array1 = array('marko','aca','milos'); 
$array2 = array('nemanja','marko','milos'); 
for($i=0;$i<count($array2);$i++)//count any array 
{ 
if($array1[$i]!=$array2[$i]) 
{ 
    echo "Error in ".$i." <br/>";/*it will show you a number of part of array wich don't match */ 
} 
} 

cela donnera
Erreur dans 0
Erreur dans 1

+1

Merci, mais même commentaire que l'autre réponse. J'ai un nombre indéfini de tableaux. Alors peut-être il y a 3 tableaux, une autre fois il y aura 5 tableaux, ... – Michiel

+0

Je pense que de cette façon vous aurez ce dont vous avez besoin. Placez simplement vos tableaux à la place des miens, et dans if, comparez $ array1 [$ i] ['value'] avec array2 [$ i] ['value'] au lieu du mien. –

+0

vous pouvez compter tableau principal (tableau parent de tous les tableaux.) Savez-vous ce que je veux dire? –

Questions connexes