2

I have an Array $testes that recieves two arrays $variavel1 and $variavel2. Then I set:

$this->set('testes', $testes)

In View How I can Take the values from $variavel1 and $variavel2 ?

I tried $testes['$variavel1']['field'], but I got Undefined Index $variavel1

Practical Example:

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

and

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

And i'm setting $testes like this, $testes = array($variavel1, $variavel2);

Images to explain my problem: enter image description here enter image description here enter image description here

12
  • this wont work like this $testes['$variavel1']['field'] , maybe $testes[$variavel1]['field'] ? Commented May 6, 2015 at 16:40
  • Tira as aspas simples quando tiver uma variável . Commented May 6, 2015 at 16:41
  • Try $testes[0]['ProcuraProdutoPedOnline'] Commented May 6, 2015 at 16:42
  • I already tried all this ! And I got : Undefined Index... With this $testes[$variavel1]['field'], I got this : Use of undefined constant VwEstPedOnline - assumed 'VwEstPedOnline' @PedroLobito, Já tentei de diversas formas ! Inclusive essa que voce me disse, mas não funcionou. Commented May 6, 2015 at 16:46
  • Please be more specific or show some code. What are your examples? $variavel1 and $variavel2? Or 2 examples for $testes? Which value do you want to retrieve from this example? Commented May 6, 2015 at 16:48

1 Answer 1

3

I think you first need to know the array structure. Add this to your view and you'll get a list:

echo '<pre>';
print_r($testes);
echo '</pre>';

If the result is empty the problem might be in the place where you add $variavel1 and $variavel2 to the variable $testes...

EDIT 2: By your example Array:

// Result should be 2034
echo $testes['0']['0']['VwEstPedOnline']['cd_seq_pedido'];

EDIT 3 as answer to your question in the comments to this post:

I still don't understand how $variavel1 and $variavel2 are associated. If there is no connection, you don't need to merge them into one array. You can simply use one foreach within another foreach:

In your controller:

$familias = $this->ProcuraProdutoPedOnline->find('all', array(
  'fields' => array('cd_familia', 'ds_familia'),
  'order' => 'cd_familia'));
$this->set('familias', $familias);

$cdSeqPeds = $this->VwEstPedOnline->find('all', array(
  'fields' => 'cd_seq_pedido',
  'order' => 'cd_seq_pedido'));
$this->set('cdSeqPeds', $cdSeqPeds);

And in your view:

foreach ( $familias as $var1 ) {
  echo '<p>'.$var1['ProcuraProdutoPedOnline']['ds_familia'].':</p>';
  echo '<ul>';
  foreach ( $cdSeqPeds as $var2 ) {
    echo '<li>'.$var2['VwEstPedOnline']['cd_seq_pedido'].'</li>';
  }
  echo '</ul>';
}

The result shoul be something like this:

ACESSORIOS:
· 2034
· 2038
CALCADOS:
· 2034
· 2038

If this doesn't answer your question, please make an example (with english variable names) what you want to get exactly and how the two tables are connected/associated...

Sign up to request clarification or add additional context in comments.

14 Comments

Isn't empty, the value is there, this is the structure : Array ( [0] => Array ( [0] => Array ( [VwEstPedOnline] => Array ( [cd_seq_pedido] => 2034 ) ) [1] => Array ( [VwEstPedOnline] => Array ( [cd_seq_pedido] => 2038 ) )
Yes, It worked ! But result 2034 comes duplicated and I want to bring all the Array, understand ? All positions. How can I do that ?
foreach ( $testes['0'] as $item ) { echo '<p>'.$item['VwEstPedOnline']['cd_seq_pedido'].'</p>'; }
And If I want the position [1] on the same foreach, is that possible ??? Whats the difference of foreach($variavel as $obj) to foreach($variavel as key=> $obj) ??? Thanks a lot for the help.
No, sorry, I'm out for today, it's late... I gave you multiple hints, please have a look at them. If you currently don't see a solution, please start a new question on Stackoverflow and give a clear (english) example what you have (tables, connections), what you get and what you want to get (with examples)... Good luck! And good night.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.