有 2 个 php 数组。
一个数组是从输入值创建的(数组名为$date_and_currency_array_for_comparision
)。
Array
(
[0] => Array
(
[CurrencyAbbreviation] => USD
[DateOfCurrencyRate] => 2013-07-22
)
[1] => Array
(
[CurrencyAbbreviation] => CAD
[DateOfCurrencyRate] => 2013-07-11
)
[3] => Array
(
[CurrencyAbbreviation] => CZK
[DateOfCurrencyRate] => 2013-07-31
)
)
另一个数组是从 mysql (named data_select_currency_rate
)中的值创建的
Array
(
[0] => Array
(
[CurrencyAbbreviation] => CAD
[DateOfCurrencyRate] => 2013-07-11
)
[1] => Array
(
[CurrencyAbbreviation] => CZK
[DateOfCurrencyRate] => 2013-07-31
)
)
如果第二个数组中不存在这些值,则需要使用第一个数组中的值创建数组。
起初尝试
$currencies_that_must_insert_into_mysql = array_diff_assoc($date_and_currency_array_for_comparision, $data_select_currency_rate);
但得到不正确的结果。例如知道在 mysql 中没有[CurrencyAbbreviation] => USD
但$currencies_that_must_insert_into_mysql
显示[CurrencyAbbreviation] => CZK
.
然后尝试了这个
$data_difference = array();
foreach ($date_and_currency_array_for_comparision as $key_longer_array => $value_longer_array){
foreach ($data_select_currency_rate as $key_shorter_array => $value_shorter_array){
if (
($value_longer_array[CurrencyAbbreviation] != $value_shorter_array[CurrencyAbbreviation])
and ($value_longer_array[DateOfCurrencyRate] != $value_shorter_array[DateOfCurrencyRate]) ) {
$data_difference[$key_longer_array][CurrencyAbbreviation] = $value_longer_array[CurrencyAbbreviation];
$data_difference[$key_longer_array][DateOfCurrencyRate] = $value_longer_array[DateOfCurrencyRate];
}
}
}
但是,如果第二个数组是空白的(mysql 中没有值),那么print_r($data_difference)
也不会显示任何内容。并且可能还有其他问题。
请建议如何使用存在于较长数组中且不存在于较短数组中的值创建数组