0

I have 2 lists of countries: the default directory/country list and another one from an extension. I want to display only the values from the Magento list that are common between the two values. The reason why I want to do this is because the list from the extension is prone to have typos, whilst on the other hand the list from Magento can get translated to the store view language.

I thought I might give array_intersect a try, but Magento throws the following warning:

Warning: array_intersect(): Argument #1 is not an array

The code that I have is

class Company_Module_IndexController extends Mage_Core_Controller_Front_Action {
    public function indexAction() {
        //list from the extension with country names
        $mapcountries =   Mage::getModel('gmapstrlocator/gmapstrlocator')->getCollection();
        foreach ($mapcountries as $mapcountry) {
            $mapcountry = $mapcountry->getData('country');
        }
        //list from Magento with country names instead of iso codes
        $magecountries = Mage::getModel('directory/country')->getCollection();
        foreach ($magecountries as $country) {
            $cn = $country->getName();
        }
        //display the intersection between both
        $finalcountries = array_intersect($mapcountry, $cn);
        print_r($finalcountries);

    }
}

How can I achieve this? Maybe array_intersect isn't a good option and you guys have other ideas.

Thanks.

2

This is actually a simple PHP issue :-) Try this one:

class Company_Module_IndexController extends Mage_Core_Controller_Front_Action {
    public function indexAction() {
        //list from the extension with country names
        $mapcountries =   Mage::getModel('gmapstrlocator/gmapstrlocator')->getCollection();
        $mapcountriesArray = array();
        foreach ($mapcountries as $mapcountry) {
            $mapcountriesArray[] = $mapcountry->getData('country');
        }
        //list from Magento with country names instead of iso codes
        $magecountries = Mage::getModel('directory/country')->getCollection();
        $magecountriesArray = array();
        foreach ($magecountries as $country) {
            $magecountriesArray[] = $country->getName();
        }
        //display the intersection between both
        $finalcountries = array_intersect($mapcountriesArray, $magecountriesArray);
        print_r($finalcountries);

    }
}

Mind that it may be slow to iterate both collections and put them into an array, but I doubt there is another possibility since you also need to call a method on the items... In any case, consider implementing a proper cache for that.

  • Thanks for your help Simon. Your code works. The only caveat is that it displays United Kingdom several times as several of the distributors are based in there. Is there any possible way to display United Kingdom just once? – VDD Mar 6 '15 at 18:07
  • The idea behind all this is to use the country values in a form with a dropdown, so it'd be perfect if I could achieve that. – VDD Mar 6 '15 at 18:14
  • 1
    $finalcountries = array_intersect($magecountriesArray, $mapcountriesArray); does exactly what I want. Thanks. – VDD Mar 6 '15 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.