我的下面有一个关联数组:
<?php
$first = array(
array('lastexam' => '170220', 'nextexam' => '170220', 'phone' => '170220', 'fax' => '170220'),
array('lastexam' => '170220', 'nextexam' => '170220', 'phone' => '170220', 'fax' => '170220'),
array('lastexam' => '170221', 'nextexam' => '170220', 'phone' => '170224', 'fax' => '170220'),
array('lastexam' => '170221', 'nextexam' => '170226', 'phone' => '170220', 'fax' => '170220'),
array('lastexam' => '170222', 'nextexam' => '170220', 'phone' => '170225', 'fax' => '170220')
);
?>
我需要将值$first[0]
与$first[1]
,$first[1]
与$first[2]
,$first[2]
与$first[3]
以及{{1}进行比较到$first[3]
。我需要得到如下结果:
例如:
$first[4]
答案 0 :(得分:1)
我认为这可以解决您的问题
<?php
$first = array(
array('lastexam' => '170220', 'nextexam' => '170220', 'phone' => '170220', 'fax' => '170220'),
array('lastexam' => '170220', 'nextexam' => '170220', 'phone' => '170220', 'fax' => '170220'),
array('lastexam' => '170221', 'nextexam' => '170220', 'phone' => '170224', 'fax' => '170220'),
array('lastexam' => '170221', 'nextexam' => '170226', 'phone' => '170220', 'fax' => '170220'),
array('lastexam' => '170222', 'nextexam' => '170220', 'phone' => '170225', 'fax' => '170220')
);
$pre = array();
$cur = array();
$tmp = array();
echo '<pre>'; print_r($first); echo '</pre>';
foreach($first as $key => $val) {
foreach ($val as $key1 => $val1) {
$cur[$key1] = $val1;
if (isset($pre[$key1]) && $pre[$key1] !== $cur[$key1]) {
$tmp[$key][$key1] = $val1;
}
$pre[$key1] = $val1;
}
}
echo '<pre>'; print_r($tmp); echo '</pre>';
?>
如果是的话请在下面注明
答案 1 :(得分:1)
我发现布拉德肯特在评论中使用array_diff
要清楚得多。
$result = [];
for ($i = count($first)-1; $i >= 1; $i--) {
$diff = array_diff($first[$i], $first[$i-1]);
if (!empty($diff))
$result[$i] = $diff;
}
答案 2 :(得分:0)
这有点像你在寻找什么?
$first = array(
array('lastexam' => '170220', 'nextexam' => '170220', 'phone' => '170220', 'fax' => '170220'),
array('lastexam' => '170220', 'nextexam' => '170220', 'phone' => '170220', 'fax' => '170220'),
array('lastexam' => '170221', 'nextexam' => '170220', 'phone' => '170224', 'fax' => '170220')
);
if((array_values($first)[0]['lastexam']) == (array_values($first)[1]['lastexam'])){
var_dump(array_values($first)[2]['lastexam']);
var_dump(array_values($first)[2]['phone']);
} else {
//
echo "whatever..." ;
//
}
??还不完美,因为我没有掌握你想要实现的全局。但是,通过这个if语句,您可以至少比较您想要比较的内容。祝你好运!