我有一个foreach
语法,可以产生以下结果:
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
...
...
代表了更多代码,所有代码都具有相同的结构。
以下是php
代码:
foreach ($result as $term) {
$node = node_load($term->nid);
$dates = $node->field_date['und'];
var_dump($dates);
}
目标是比较结果并检查它们是否存在任何不等式。所以我的目标是一个脚本,它允许我比较第一个数组的$dates[0]['value']
和第二个数组的$dates[0]['value']
......
我正在考虑用所有0
项或所有1
项组成新数组,然后检查它们,但到目前为止还没有运气。有人有另一个想法吗?
答案 0 :(得分: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";
输出:Error key = value2
编辑:更改以反映对该问题的新理解。
答案 1 :(得分: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
* )
*/
答案 2 :(得分:0)
我认为这是一种方式
$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 */
}
}
这将给予 0中的错误 错误1