1

Have 2 php arrays.

One array is created from input values (array named $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
        )

)

Another array is created from values in mysql (named data_select_currency_rate)

Array
(
    [0] => Array
        (
            [CurrencyAbbreviation] => CAD
            [DateOfCurrencyRate] => 2013-07-11
        )

    [1] => Array
        (
            [CurrencyAbbreviation] => CZK
            [DateOfCurrencyRate] => 2013-07-31
        )

)

Need to create array with values from first array if these values does not exist in the second array.

At first tried

$currencies_that_must_insert_into_mysql = array_diff_assoc($date_and_currency_array_for_comparision, $data_select_currency_rate);

But get incorrect results. For example know that in mysql there is no [CurrencyAbbreviation] => USD but $currencies_that_must_insert_into_mysql shows [CurrencyAbbreviation] => CZK.

Then tried this

$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];

}

  }

   }

But, if the second array is blank (no values in mysql), then print_r($data_difference) also shows nothing. And possibly there are other problems.

Please advice how to create array with values that exists in longer array and does not exists in shorter array

5
  • 1
    Think you are just missing some quotes, $value_longer_array[CurrencyAbbreviation] should be $value_longer_array['CurrencyAbbreviation'] Commented Aug 26, 2013 at 14:58
  • please put your code in phpfiddle.com and give us a link to test with you Commented Aug 26, 2013 at 15:03
  • @FaceOfJock I put it here xfiddle.com/main/php/one_array_from_two_30465aca.php but... errors Undefined index. Actually it works not the same way as on website Commented Aug 26, 2013 at 15:35
  • give me only code of your arrays,values. Commented Aug 26, 2013 at 15:38
  • $date_and_currency_array_for_comparision = array ( array("CurrencyAbbreviation",'USD','CAD','CZK'), array("DateOfCurrencyRate",'2013-07-22','2013-07-11','2013-07-31'), ); and $data_select_currency_rate = array ( array("CurrencyAbbreviation",'CAD','CZK'), array("DateOfCurrencyRate",'2013-07-11','2013-07-31'), ); If i correctly understand what is necessary Commented Aug 26, 2013 at 15:41

2 Answers 2

1

You almost got it. You just need to count the number of elements in second array. If it is zero, just assign the first array to $data_difference. Otherwise, perform the loop you have.

Also, remember to quote the keys of your array when they are in string format.

$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'];

         }

      }

   }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Appears very simple. I even can not believe, that there are no "underwater rocks". At first sight all works as necessary, but after some days when work with other part of code, appears, that previously created code is not correct... and need to "go back" and correct
There is problem in my logic. If 'CurrencyAbbreviation' AND 'DateOfCurrencyRate' is not the same. Hovewer in mysql there may be the same 'DateOfCurrencyRate' and not the same 'CurrencyAbbreviation'. OR also can not help. Seems need to combine the both values from input and mysql and then check the combined value....
Do you mean you want to filter the value with the same 'CurrencyAbbreviation' but different 'DateOfCurrencyRate'? In that case, in your if() statement, just remove the and ($value_longer_array['DateOfCurrencyRate'] != $value_shorter_array['DateOfCurrencyRate']) part. Otherwise, you might need to post more details for us to see.
I mean, for example, in mysql there may be only value for USD 2013-07-01 and my input may be USD 2013-07-02. As in mysql there is no such value I need to insert it. But with condition AND this value would not be in $data_difference. So at the moment decided combine values, for example, like USD2013-07-02 and USD2013-07-01 and then in if to compare the combined/united values
In that case, create two arrays to store the values you want to insert to your database. First one is $data_difference, which is what we already have above. The second one, let's call it $data_new. This will be value with same currency different date. You can get it by checking for item with same CurrencyAbbreviation but different DateOfCurrencyRate.
|
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];
}

 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.