Problems with PHP Array

Asked

Viewed 56 times

0

I’m trying to work with arrays but I’m having difficulty accessing the items or they see grouped or they generate several arrays, which I can’t access.

CONTROLLER

$listaDescontos = $dados['listTabelaDesconto'];
            foreach($listaDescontos as $key => $value){
               $listDesc = $value['usu_tabdsc'];

               $dados['listFPG'] = (new Step3Model)->getAllFormaPagamentoLocal($_SESSION['filial'],$listDesc);       

               $list = $dados['listFPG'];           

               foreach($list as $keyList ){                
                $teste = $keyList;
                print_r( $teste); 
                //$dados['listFormaPgto'] = $valueList;          
               }                
            }   

VIEW

 foreach($listFormaPgto as $item) {

                                if(isset($_POST['formaPgto']) && $_POST['formaPgto'] == $item['codfpg']){
                                    $selected = 'selected';
                                } else if(isset($currentPedido) && $currentPedido['codfpg'] == $item['codfpg']){
                                    $selected = 'selected';
                                } else { 
                                    $selected = '';
                                }

                                echo '<option value="'.$item['codfpg'].'" '.$selected.'>'.$item['desfpg'].'</option>';
                            }

RESULTING ARRAY

Array ( [codfpg] => 9 [0] => 9 [desfpg] => VALE OF [1] => VALE OF [usu_moefpg] => AU [2] => AU ) Array ( [codfpg] => 1 [0] => 1 [desfpg] => Cobranca Bancaria [1] => Cobranca Bancaria [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 3 [0] => 3 [desfpg] => Deposito em Conta [1] => Deposito em Conta [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 5 [0] => 5 [desfpg] => Dinheiro [1] => Dinheiro [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 6 [0] => 6 [desfpg] => VALE RS [1] => VALE RS [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 7 [0] => 7 [desfpg] => CHEQUE [1] => CHEQUE [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 10 [0] => 10 [desfpg] => Cartao Debito [1] => Cartao Debito [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 11 [0] => 11 [desfpg] => Cartao Credito [1] => Cartao Credito [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 99 [0] => 99 [desfpg] => Crediario [1] => Crediario [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 1 [0] => 1 [desfpg] => Cobranca Bancaria [1] => Cobranca Bancaria [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 3 [0] => 3 [desfpg] => Deposito em Conta [1] => Deposito em Conta [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 5 [0] => 5 [desfpg] => Dinheiro [1] => Dinheiro [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 6 [0] => 6 [desfpg] => VALE RS [1] => VALE RS [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 7 [0] => 7 [desfpg] => CHEQUE [1] => CHEQUE [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 10 [0] => 10 [desfpg] => Cartao Debito [1] => Cartao Debito [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 11 [0] => 11 [desfpg] => Cartao Credito [1] => Cartao Credito [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 99 [0] => 99 [desfpg] => Crediario [1] => Crediario [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 1 [0] => 1 [desfpg] => Cobranca Bancaria [1] => Cobranca Bancaria [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 3 [0] => 3 [desfpg] => Deposito em Conta [1] => Deposito em Conta [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 5 [0] => 5 [desfpg] => Dinheiro [1] => Dinheiro [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 6 [0] => 6 [desfpg] => VALE RS [1] => VALE RS [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 7 [0] => 7 [desfpg] => CHEQUE [1] => CHEQUE [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 10 [0] => 10 [desfpg] => Cartao Debito [1] => Cartao Debito [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 11 [0] => 11 [desfpg] => Cartao Credito [1] => Cartao Credito [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 99 [0] => 99 [desfpg] => Crediario [1] => Crediario [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 1 [0] => 1 [desfpg] => Cobranca Bancaria [1] => Cobranca Bancaria [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 3 [0] => 3 [desfpg] => Deposito em Conta [1] => Deposito em Conta [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 5 [0] => 5 [desfpg] => Dinheiro [1] => Dinheiro [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 6 [0] => 6 [desfpg] => VALE RS [1] => VALE RS [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 7 [0] => 7 [desfpg] => CHEQUE [1] => CHEQUE [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 10 [0] => 10 [desfpg] => Cartao Debito [1] => Cartao Debito [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 11 [0] => 11 [desfpg] => Cartao Credito [1] => Cartao Credito [usu_moefpg] => 01 [2] => 01 ) Array ( [codfpg] => 99 [0] => 99 [desfpg] => Crediario [1] => Crediario [usu_moefpg] => 01 [2] => 01 )

I need to get the information like desfpg, codfpg, usu_moefpg in variables but I’m not getting success proceeding this way. HELP!

1 answer

0

Try it this way, without using foreach, even though you know it is more suitable

$array = array(
    'stack' => 'Overflow',
    'linguagem' => 'Português',
    'tags' => array('PHP', 'Iteração', 'Array')
);

$keys = array_keys($array);

$size = count($array);

for ($i = 0; $i < $size; $i++) {
    $key   = $keys[$i];
    $value = $array[$key];

    echo $value;

}

or even so:

$lista = ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sabado"];
$qnt = count($lista);
echo $qnt . "\n";

for($x=0; $x<$qnt; $x++){
    echo "$x: $lista[$x]\n";
}

links to support:

link 1 link 2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.