如何创建具有较长数组中存在的值的数组,并且在较短的数组中不存在?

时间:2013-08-26 14:49:18

标签: php

有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(名为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)也没有显示任何内容。可能还有其他问题。

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

2 个答案:

答案 0 :(得分: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];
}

 }

答案 1 :(得分:1)

你几乎得到了它。你只需要count the number of elements in second array。如果它为零,只需将第一个数组分配给$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'];

         }

      }

   }
}