1

有 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)也不会显示任何内容。并且可能还有其他问题。

请建议如何使用存在于较长数组中且不存在于较短数组中的值创建数组

4

2 回答 2

1
$data_difference = array();
foreach ($date_and_currency_array_for_comparision as $key_longer_array =>$value_longer_array){

 if(count($data_select_currency_rate)>0)
 {
 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];

}

 }

  }
 else
 {
   $data_difference[$key_longer_array][CurrencyAbbreviation] = $value_longer_array[CurrencyAbbreviation];
   $data_difference[$key_longer_array][DateOfCurrencyRate] = $value_longer_array[DateOfCurrencyRate];
}

 }
于 2013-08-26T15:03:37.213 回答
1

你几乎明白了。您只需要计算第二个数组中的元素数。如果它为零,只需将第一个数组分配给$data_difference. 否则,请执行您拥有的循环。

另外,请记住在数组的键为字符串格式时引用它们。

$data_difference = array();

// If second array is empty, put the contents of first array into $data_difference.
if (0 == count ($data_select_currency_rate))
{
   $data_difference = $date_and_currency_array_for_comparison;
}
else
{
   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)
      {
         // Remember to quote the key/index of the 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'];

         }

      }

   }
}
于 2013-08-26T15:05:10.767 回答