php - How to create array with values that exists in longer array and does not exists in shorter array? -
have 2 php arrays.
one array created 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 created 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 create array values first array if these values not exist in 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 incorrect results. example know in mysql there no [currencyabbreviation] => usd
$currencies_that_must_insert_into_mysql
shows [currencyabbreviation] => czk
.
then tried this
$data_difference = array(); foreach ($date_and_currency_array_for_comparision $key_longer_array => $value_longer_array){ foreach ($data_select_currency_rate $key_shorter_array => $value_shorter_array){ if ( ($value_longer_array[currencyabbreviation] != $value_shorter_array[currencyabbreviation]) , ($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 second array blank (no values in mysql), print_r($data_difference)
shows nothing. , possibly there other problems.
please advice how create array values exists in longer array , not exists in shorter array
you got it. need count number of elements in second array. if zero, assign first array $data_difference
. otherwise, perform loop have.
also, remember quote keys of array when in string format.
$data_difference = array(); // if second array empty, put contents of first array $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 $key_longer_array => $value_longer_array) { foreach ($data_select_currency_rate $key_shorter_array => $value_shorter_array) { // remember quote key/index of array. if ( ($value_longer_array['currencyabbreviation'] != $value_shorter_array['currencyabbreviation']) , ($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']; } } } }
Comments
Post a Comment