php - insert arrays on keys of other arrays. - a simple array request
$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('configoption开发者_运维问答s');
$precosOpcao = $servicoDao->precos($servicoVo);
}
Thanks a lot, MEM
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);
}
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.
精彩评论