Error listing items in an array with foreach ()

4

I have array with the list of several Brazilian banks and I need to get the name of the bank where the code of it is passed according to what it has in the user's account, but when doing this search for the code it returns me an error in foreach :

<?php

$bancos = array(
    array('code' => '001', 'name' => 'Banco do Brasil'),
    array('code' => '003', 'name' => 'Banco da Amazônia'),
    array('code' => '004', 'name' => 'Banco do Nordeste'),
    array('code' => '021', 'name' => 'Banestes'),
    array('code' => '025', 'name' => 'Banco Alfa'),
    array('code' => '027', 'name' => 'Besc'),
    array('code' => '029', 'name' => 'Banerj'),
    array('code' => '031', 'name' => 'Banco Beg'),
    array('code' => '033', 'name' => 'Banco Santander Banespa'),
    array('code' => '036', 'name' => 'Banco Bem'),
    array('code' => '037', 'name' => 'Banpará'),
    array('code' => '038', 'name' => 'Banestado'),
    array('code' => '039', 'name' => 'BEP'),
    array('code' => '040', 'name' => 'Banco Cargill'),
    array('code' => '041', 'name' => 'Banrisul'),
    array('code' => '044', 'name' => 'BVA'),
    array('code' => '045', 'name' => 'Banco Opportunity'),
    array('code' => '047', 'name' => 'Banese'),
    array('code' => '062', 'name' => 'Hipercard'),
    array('code' => '063', 'name' => 'Ibibank'),
    array('code' => '065', 'name' => 'Lemon Bank'),
    array('code' => '066', 'name' => 'Banco Morgan Stanley Dean Witter'),
    array('code' => '069', 'name' => 'BPN Brasil'),
    array('code' => '070', 'name' => 'Banco de Brasília – BRB'),
    array('code' => '072', 'name' => 'Banco Rural'),
    array('code' => '073', 'name' => 'Banco Popular'),
    array('code' => '074', 'name' => 'Banco J. Safra'),
    array('code' => '075', 'name' => 'Banco CR2'),
    array('code' => '076', 'name' => 'Banco KDB'),
    array('code' => '096', 'name' => 'Banco BMF'),
    array('code' => '104', 'name' => 'Caixa Econômica Federal'),
    array('code' => '107', 'name' => 'Banco BBM'),
    array('code' => '116', 'name' => 'Banco Único'),
    array('code' => '151', 'name' => 'Nossa Caixa'),
    array('code' => '175', 'name' => 'Banco Finasa'),
    array('code' => '184', 'name' => 'Banco Itaú BBA'),
    array('code' => '204', 'name' => 'American Express Bank'),
    array('code' => '208', 'name' => 'Banco Pactual'),
    array('code' => '212', 'name' => 'Banco Matone'),
    array('code' => '213', 'name' => 'Banco Arbi'),
    array('code' => '214', 'name' => 'Banco Dibens'),
    array('code' => '217', 'name' => 'Banco Joh Deere'),
    array('code' => '218', 'name' => 'Banco Bonsucesso'),
    array('code' => '222', 'name' => 'Banco Calyon Brasil'),
    array('code' => '224', 'name' => 'Banco Fibra'),
    array('code' => '225', 'name' => 'Banco Brascan'),
    array('code' => '229', 'name' => 'Banco Cruzeiro'),
    array('code' => '230', 'name' => 'Unicard'),
    array('code' => '233', 'name' => 'Banco GE Capital'),
    array('code' => '237', 'name' => 'Bradesco'),
    array('code' => '241', 'name' => 'Banco Clássico'),
    array('code' => '243', 'name' => 'Banco Stock Máxima'),
    array('code' => '246', 'name' => 'Banco ABC Brasil'),
    array('code' => '248', 'name' => 'Banco Boavista Interatlântico'),
    array('code' => '249', 'name' => 'Investcred Unibanco'),
    array('code' => '250', 'name' => 'Banco Schahin'),
    array('code' => '252', 'name' => 'Fininvest'),
    array('code' => '254', 'name' => 'Paraná Banco'),
    array('code' => '263', 'name' => 'Banco Cacique'),
    array('code' => '265', 'name' => 'Banco Fator'),
    array('code' => '266', 'name' => 'Banco Cédula'),
    array('code' => '300', 'name' => 'Banco de la Nación Argentina'),
    array('code' => '318', 'name' => 'Banco BMG'),
    array('code' => '320', 'name' => 'Banco Industrial e Comercial'),
    array('code' => '356', 'name' => 'ABN Amro Real'),
    array('code' => '341', 'name' => 'Itau'),
    array('code' => '347', 'name' => 'Sudameris'),
    array('code' => '351', 'name' => 'Banco Santander'),
    array('code' => '353', 'name' => 'Banco Santander Brasil'),
    array('code' => '366', 'name' => 'Banco Societe Generale Brasil'),
    array('code' => '370', 'name' => 'Banco WestLB'),
    array('code' => '376', 'name' => 'JP Morgan'),
    array('code' => '389', 'name' => 'Banco Mercantil do Brasil'),
    array('code' => '394', 'name' => 'Banco Mercantil de Crédito'),
    array('code' => '399', 'name' => 'HSBC'),
    array('code' => '409', 'name' => 'Unibanco'),
    array('code' => '412', 'name' => 'Banco Capital'),
    array('code' => '422', 'name' => 'Banco Safra'),
    array('code' => '453', 'name' => 'Banco Rural'),
    array('code' => '456', 'name' => 'Banco Tokyo Mitsubishi UFJ'),
    array('code' => '464', 'name' => 'Banco Sumitomo Mitsui Brasileiro'),
    array('code' => '477', 'name' => 'Citibank'),
    array('code' => '479', 'name' => 'Itaubank (antigo Bank Boston)'),
    array('code' => '487', 'name' => 'Deutsche Bank'),
    array('code' => '488', 'name' => 'Banco Morgan Guaranty'),
    array('code' => '492', 'name' => 'Banco NMB Postbank'),
    array('code' => '494', 'name' => 'Banco la República Oriental del Uruguay'),
    array('code' => '495', 'name' => 'Banco La Provincia de Buenos Aires'),
    array('code' => '505', 'name' => 'Banco Credit Suisse'),
    array('code' => '600', 'name' => 'Banco Luso Brasileiro'),
    array('code' => '604', 'name' => 'Banco Industrial'),
    array('code' => '610', 'name' => 'Banco VR'),
    array('code' => '611', 'name' => 'Banco Paulista'),
    array('code' => '612', 'name' => 'Banco Guanabara'),
    array('code' => '613', 'name' => 'Banco Pecunia'),
    array('code' => '623', 'name' => 'Banco Panamericano'),
    array('code' => '626', 'name' => 'Banco Ficsa'),
    array('code' => '630', 'name' => 'Banco Intercap'),
    array('code' => '633', 'name' => 'Banco Rendimento'),
    array('code' => '634', 'name' => 'Banco Triângulo'),
    array('code' => '637', 'name' => 'Banco Sofisa'),
    array('code' => '638', 'name' => 'Banco Prosper'),
    array('code' => '643', 'name' => 'Banco Pine'),
    array('code' => '652', 'name' => 'Itaú Holding Financeira'),
    array('code' => '653', 'name' => 'Banco Indusval'),
    array('code' => '654', 'name' => 'Banco A.J. Renner'),
    array('code' => '655', 'name' => 'Banco Votorantim'),
    array('code' => '707', 'name' => 'Banco Daycoval'),
    array('code' => '719', 'name' => 'Banif'),
    array('code' => '721', 'name' => 'Banco Credibel'),
    array('code' => '734', 'name' => 'Banco Gerdau'),
    array('code' => '735', 'name' => 'Banco Pottencial'),
    array('code' => '738', 'name' => 'Banco Morada'),
    array('code' => '739', 'name' => 'Banco Galvão de Negócios'),
    array('code' => '740', 'name' => 'Banco Barclays'),
    array('code' => '741', 'name' => 'BRP'),
    array('code' => '743', 'name' => 'Banco Semear'),
    array('code' => '745', 'name' => 'Banco Citibank'),
    array('code' => '746', 'name' => 'Banco Modal'),
    array('code' => '747', 'name' => 'Banco Rabobank International'),
    array('code' => '748', 'name' => 'Banco Cooperativo Sicredi'),
    array('code' => '749', 'name' => 'Banco Simples'),
    array('code' => '751', 'name' => 'Dresdner Bank'),
    array('code' => '752', 'name' => 'BNP Paribas'),
    array('code' => '753', 'name' => 'Banco Comercial Uruguai'),
    array('code' => '755', 'name' => 'Banco Merrill Lynch'),
    array('code' => '756', 'name' => 'Banco Cooperativo do Brasil'),
    array('code' => '757', 'name' => 'KEB'),
);

function Bancos(){

global $bancos;

return $bancos;

}

function BancoPorID($id){

global $bancos;

   foreach($bancos as $banco){

        if($banco['code'] == $id){

            return $banco['name'];
        }
   }
}

I call the function like this:

<?php echo BancoPorID($this->conta_model->user('banco')); //Aqui ele informa o CODE que esta no banco de dados ?>

Error returning:

  

Message: Invalid argument supplied for foreach ()

    
asked by anonymous 28.12.2015 / 18:52

3 answers

3

If you really need the nested structure presented in the question, you can simplify it like this:

function BancoPorID($id){
   global $bancos;
   $key = array_search( $id, array_column( $bancos, 'code') );
   return $key !== false ? $bancos[$key]['name']:'Banco desconhecido';
}

See working on IDEONE with the same structure as your code.

Alternatively, if you can restructure the array , a simplification tip follows:

$bancos = array(
    '001' => 'Banco do Brasil'),
    '003' => 'Banco da Amazônia'),
    ... etc ...
    '757' => 'KEB'),
);

function BancoPorID($id){
   global $bancos;
   return isset( $bancos[$id] ) ? $bancos[$id] : 'Banco desconhecido';
}
    
28.12.2015 / 19:17
1

An alternative:

function BancoPorID($code)
{
    global $bancos;
    foreach($bancos as $banco){
        if(isset($banco['code']) && $banco['code'] == $code){
            return $banco['name'];
        }
        continue;
    }
    return 'Desconhecido';
}

var_export(BancoPorID('001'));

With return within loop you can also end the cycle, and return what you want.

If the purpose is to return only the bank name, simplify as @Bacco mentioned in the second sample example would be enough.

    
28.12.2015 / 20:03
0

SOLUTION

I was able to sort by setting the variable $bancos to global out of the functions, before setting the value of the variable $bancos

<?php

global $bancos; //Isso que adicionei

$bancos = array(
    array('code' => '001', 'name' => 'Banco do Brasil'),
    array('code' => '003', 'name' => 'Banco da Amazônia'),
    array('code' => '004', 'name' => 'Banco do Nordeste'),
    array('code' => '021', 'name' => 'Banestes'),
    array('code' => '025', 'name' => 'Banco Alfa'),
    array('code' => '027', 'name' => 'Besc'),
    array('code' => '029', 'name' => 'Banerj'),
    array('code' => '031', 'name' => 'Banco Beg'),
    array('code' => '033', 'name' => 'Banco Santander Banespa'),
    array('code' => '036', 'name' => 'Banco Bem'),
    array('code' => '037', 'name' => 'Banpará'),
    array('code' => '038', 'name' => 'Banestado'),
    array('code' => '039', 'name' => 'BEP'),
    array('code' => '040', 'name' => 'Banco Cargill'),
    array('code' => '041', 'name' => 'Banrisul'),
    array('code' => '044', 'name' => 'BVA'),
    array('code' => '045', 'name' => 'Banco Opportunity'),
    array('code' => '047', 'name' => 'Banese'),
    array('code' => '062', 'name' => 'Hipercard'),
    array('code' => '063', 'name' => 'Ibibank'),
    array('code' => '065', 'name' => 'Lemon Bank'),
    array('code' => '066', 'name' => 'Banco Morgan Stanley Dean Witter'),
    array('code' => '069', 'name' => 'BPN Brasil'),
    array('code' => '070', 'name' => 'Banco de Brasília – BRB'),
    array('code' => '072', 'name' => 'Banco Rural'),
    array('code' => '073', 'name' => 'Banco Popular'),
    array('code' => '074', 'name' => 'Banco J. Safra'),
    array('code' => '075', 'name' => 'Banco CR2'),
    array('code' => '076', 'name' => 'Banco KDB'),
    array('code' => '096', 'name' => 'Banco BMF'),
    array('code' => '104', 'name' => 'Caixa Econômica Federal'),
    array('code' => '107', 'name' => 'Banco BBM'),
    array('code' => '116', 'name' => 'Banco Único'),
    array('code' => '151', 'name' => 'Nossa Caixa'),
    array('code' => '175', 'name' => 'Banco Finasa'),
    array('code' => '184', 'name' => 'Banco Itaú BBA'),
    array('code' => '204', 'name' => 'American Express Bank'),
    array('code' => '208', 'name' => 'Banco Pactual'),
    array('code' => '212', 'name' => 'Banco Matone'),
    array('code' => '213', 'name' => 'Banco Arbi'),
    array('code' => '214', 'name' => 'Banco Dibens'),
    array('code' => '217', 'name' => 'Banco Joh Deere'),
    array('code' => '218', 'name' => 'Banco Bonsucesso'),
    array('code' => '222', 'name' => 'Banco Calyon Brasil'),
    array('code' => '224', 'name' => 'Banco Fibra'),
    array('code' => '225', 'name' => 'Banco Brascan'),
    array('code' => '229', 'name' => 'Banco Cruzeiro'),
    array('code' => '230', 'name' => 'Unicard'),
    array('code' => '233', 'name' => 'Banco GE Capital'),
    array('code' => '237', 'name' => 'Bradesco'),
    array('code' => '241', 'name' => 'Banco Clássico'),
    array('code' => '243', 'name' => 'Banco Stock Máxima'),
    array('code' => '246', 'name' => 'Banco ABC Brasil'),
    array('code' => '248', 'name' => 'Banco Boavista Interatlântico'),
    array('code' => '249', 'name' => 'Investcred Unibanco'),
    array('code' => '250', 'name' => 'Banco Schahin'),
    array('code' => '252', 'name' => 'Fininvest'),
    array('code' => '254', 'name' => 'Paraná Banco'),
    array('code' => '263', 'name' => 'Banco Cacique'),
    array('code' => '265', 'name' => 'Banco Fator'),
    array('code' => '266', 'name' => 'Banco Cédula'),
    array('code' => '300', 'name' => 'Banco de la Nación Argentina'),
    array('code' => '318', 'name' => 'Banco BMG'),
    array('code' => '320', 'name' => 'Banco Industrial e Comercial'),
    array('code' => '356', 'name' => 'ABN Amro Real'),
    array('code' => '341', 'name' => 'Itau'),
    array('code' => '347', 'name' => 'Sudameris'),
    array('code' => '351', 'name' => 'Banco Santander'),
    array('code' => '353', 'name' => 'Banco Santander Brasil'),
    array('code' => '366', 'name' => 'Banco Societe Generale Brasil'),
    array('code' => '370', 'name' => 'Banco WestLB'),
    array('code' => '376', 'name' => 'JP Morgan'),
    array('code' => '389', 'name' => 'Banco Mercantil do Brasil'),
    array('code' => '394', 'name' => 'Banco Mercantil de Crédito'),
    array('code' => '399', 'name' => 'HSBC'),
    array('code' => '409', 'name' => 'Unibanco'),
    array('code' => '412', 'name' => 'Banco Capital'),
    array('code' => '422', 'name' => 'Banco Safra'),
    array('code' => '453', 'name' => 'Banco Rural'),
    array('code' => '456', 'name' => 'Banco Tokyo Mitsubishi UFJ'),
    array('code' => '464', 'name' => 'Banco Sumitomo Mitsui Brasileiro'),
    array('code' => '477', 'name' => 'Citibank'),
    array('code' => '479', 'name' => 'Itaubank (antigo Bank Boston)'),
    array('code' => '487', 'name' => 'Deutsche Bank'),
    array('code' => '488', 'name' => 'Banco Morgan Guaranty'),
    array('code' => '492', 'name' => 'Banco NMB Postbank'),
    array('code' => '494', 'name' => 'Banco la República Oriental del Uruguay'),
    array('code' => '495', 'name' => 'Banco La Provincia de Buenos Aires'),
    array('code' => '505', 'name' => 'Banco Credit Suisse'),
    array('code' => '600', 'name' => 'Banco Luso Brasileiro'),
    array('code' => '604', 'name' => 'Banco Industrial'),
    array('code' => '610', 'name' => 'Banco VR'),
    array('code' => '611', 'name' => 'Banco Paulista'),
    array('code' => '612', 'name' => 'Banco Guanabara'),
    array('code' => '613', 'name' => 'Banco Pecunia'),
    array('code' => '623', 'name' => 'Banco Panamericano'),
    array('code' => '626', 'name' => 'Banco Ficsa'),
    array('code' => '630', 'name' => 'Banco Intercap'),
    array('code' => '633', 'name' => 'Banco Rendimento'),
    array('code' => '634', 'name' => 'Banco Triângulo'),
    array('code' => '637', 'name' => 'Banco Sofisa'),
    array('code' => '638', 'name' => 'Banco Prosper'),
    array('code' => '643', 'name' => 'Banco Pine'),
    array('code' => '652', 'name' => 'Itaú Holding Financeira'),
    array('code' => '653', 'name' => 'Banco Indusval'),
    array('code' => '654', 'name' => 'Banco A.J. Renner'),
    array('code' => '655', 'name' => 'Banco Votorantim'),
    array('code' => '707', 'name' => 'Banco Daycoval'),
    array('code' => '719', 'name' => 'Banif'),
    array('code' => '721', 'name' => 'Banco Credibel'),
    array('code' => '734', 'name' => 'Banco Gerdau'),
    array('code' => '735', 'name' => 'Banco Pottencial'),
    array('code' => '738', 'name' => 'Banco Morada'),
    array('code' => '739', 'name' => 'Banco Galvão de Negócios'),
    array('code' => '740', 'name' => 'Banco Barclays'),
    array('code' => '741', 'name' => 'BRP'),
    array('code' => '743', 'name' => 'Banco Semear'),
    array('code' => '745', 'name' => 'Banco Citibank'),
    array('code' => '746', 'name' => 'Banco Modal'),
    array('code' => '747', 'name' => 'Banco Rabobank International'),
    array('code' => '748', 'name' => 'Banco Cooperativo Sicredi'),
    array('code' => '749', 'name' => 'Banco Simples'),
    array('code' => '751', 'name' => 'Dresdner Bank'),
    array('code' => '752', 'name' => 'BNP Paribas'),
    array('code' => '753', 'name' => 'Banco Comercial Uruguai'),
    array('code' => '755', 'name' => 'Banco Merrill Lynch'),
    array('code' => '756', 'name' => 'Banco Cooperativo do Brasil'),
    array('code' => '757', 'name' => 'KEB'),
);

function Bancos(){

global $bancos;

return $bancos;

}

function BancoPorID($id){

global $bancos;

   foreach($bancos as $banco){

        if($banco['code'] == $id){

            return $banco['name'];
        }
   }
}
?>
    
28.12.2015 / 19:12