简体   繁体   中英

Compare different items in different arrays

I have a foreach syntax which generates following outcome:

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
      ...

The ... represent a lot more code, all with the same structure.

And here is the php code:

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

Goal is to compare the results and check them for any inequalities. So what I'm aiming for is a script which allows me to compare $dates[0]['value'] from the first array and $dates[0]['value'] from the second array...

I was thinking of composing new arrays with all the 0 items or all the 1 items and later on check them, but so far no luck. Does anybody have another idea?

$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";



Output: Error key = value2

Edit: Changes to reflect new understanding of the question.

$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
 * )
 */

I think this is a way

$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 */
}
}

this will give
Error in 0
Error in 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM