Access the data of an Array that receives 2 Arrays

2

Let's say I have an Array $testes that gets 2 arrays $variavel1 and $variavel2 . So I make $this->set('testes', $testes) to use in the view.

In the View how do I access the values of $variavel1 and $variavel2 ? I made a foreach and tried $testes['$variavel1']['campo'] , but only gives Undefined Index in $variavel1 .

Example, how to access the data in this Array?

Array (
    [0] => Array (
        [ProcuraProdutoPedOnline] => Array (
            [cd_familia] => 3
            [ds_familia] => ACESSORIOS
        )
    )
    [1] => Array (
        [ProcuraProdutoPedOnline] => Array (
            [cd_familia] => 1
            [ds_familia] => CALCADOS
        )
    )
)

E

Array (
    [0] => Array (
        [VwEstPedOnline] => Array (
            [cd_seq_pedido] => 2034
        )
    )
    [1] => Array (
        [VwEstPedOnline] => Array (
            [cd_seq_pedido] => 2038
        )
    )
)

$testes is getting like this: $testes = array($cdSeqPeds, $familias);

    
asked by anonymous 06.05.2015 / 16:58

2 answers

3

To scroll through array , do so:

$array = array("0" => array ( "ProcuraProdutoPedOnline" => array(
                              "cd_familia" => 3,
                              "ds_familia" => "ACESSORIOS")
                             ),
                "1" => array ( "ProcuraProdutoPedOnline" => array(
                               "cd_familia" => 1,
                               "ds_familia" => "CALCADOS")
                             )
               );

foreach ($array as $indices) {
    foreach($indices as $pedidos){
        foreach ($pedidos as $chave => $pedido){
            echo "$chave = $pedido\n";
        }
    }
}

To directly access a value, you need to specify the index, subarray, and key. So:

echo $array[0]["ProcuraProdutoPedOnline"]["ds_familia"];

View demonstração

    
06.05.2015 / 18:46
1

You have the answer in your own question. If there are two vectors within one, you first need to identify in which of two your object is. Of course it's important to check if the index actually exists.

For example, for the first object of your first example:

$testes[0]['ProcuraProdutoPedOnline']['cd_familia']

And for the second object:

$testes[1]['ProcuraProdutoPedOnline']['cd_familia']

Within foreach , for example:

foreach ($testes as $obj) {
    // $obj['ProcuraProdutoPedOnline']['cd_familia']
}

Naturally, no foreach does not need since it loops over elements within $testes .

    
06.05.2015 / 17:20