0

$extraServicos is an array.
I want to add a key to that array called "options" so that, on that key, I can associate another array $extraOpcoes .
On this last array I want to add yet another key called "prices" and associate one last array with it: $precosOpcao.

How can I build such an array given the following code?

$extrasServicos = $servicoDao->servicosExtras($servicoVo);

foreach ($extrasServicos as $extra) {
   $servicoVo->setExtraId($extra->id);
   $extraOpcoes = $servicoDao->extrasOpcoes($servicoVo);
   $servicoVo->setPrecoMoeda('1');
   $servicoVo->setTipoServico('configoptions');    
   $precosOpcao = $servicoDao->precos($servicoVo); 
}

Thanks a lot, MEM

1
  • could you give an example of how the array structure should look? as well as a small overview of what each of the functions do in your foreach loop? Commented Dec 29, 2010 at 18:13

2 Answers 2

2

To add a new key to an array in PHP, it is as simple as doing:

$array['new_key'] = $value;

In your example, it's hard to understand how exactly you want to add the keys. Since you are doing a loop, it looks like you may want an array of arrays. Perhaps something like this is what you are looking for:

$extrasServicos = $servicoDao->servicosExtras($servicoVo);
$extrasServicos['options'] = array();
$extrasServicos['prices'] = array();
foreach ($extrasServicos as $extra) {
   $servicoVo->setExtraId($extra->id);
   $extrasServicos['options'][] = $servicoDao->extrasOpcoes($servicoVo);
   $servicoVo->setPrecoMoeda('1');
   $servicoVo->setTipoServico('configoptions');    
   $extrasServicos['prices'][] = $servicoDao->precos($servicoVo); 
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just to clarify, you are looking for a structure such as this:

array (
    "options" => array(
        "prices" => array('10,95','21,99','30,31')
    )
)

Correct?

$extrasServicos = $servicoDao->servicosExtras($servicoVo);

foreach ($extrasServicos as $extra) {
   $servicoVo->setExtraId($extra->id);
   $extraOpcoes = $servicoDao->extrasOpcoes($servicoVo);
   $servicoVo->setPrecoMoeda('1');
   $servicoVo->setTipoServico('configoptions');    
   $precosOpcao = $servicoDao->precos($servicoVo);
   // Here is where you will put those into your overhead array
   $extraServicos['options'] = $extraOpcoes;
   $extraServicos['options']['price'] = $precosOpcao;
}

To view the structure after your run this code (to be certain it is what you are looking for) put this after the for loop (only for debug)

print"<pre>"; print_r($extraServicos);

Good luck!
Dennis M.

Comments

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.