0

I would like $servico to store all iterations of this foreach ($produtos as $produto) loop, into an array:

However, I'm getting the id, name and domain of the last occurrence only.

$servico = array();

        foreach ($resposta->products as $produtos)
        {


            foreach ($produtos as $produto)
            {

                if (!empty($produto->domain))
                {
                    $servico['id'] = $produto->id;
                    $servico['name'] = $produto->name;
                    $servico['domain'] = $produto->domain;

                    //seta o pid do produto
                    $servicoVo->setPid($produto->pid);

                    //vai buscar o gid do produto:
                    $groupId = $servicoDao->grupoServico($servicoVo);

                    //adiciona o group id ao array
                    $servico['gid'] = $groupId;
                }
            }
        }


        //devolve o array
        if (!empty($servico)) 
        {
            //echo '<pre>';
            //var_dump($servico);
            //echo '</pre>';
            return $servico;
        }

Can I have a little help here? :D

Thanks, MEM

0

3 Answers 3

1

You're rewriting the id, name, and domain keys each time. Do this instead, after of course generating $groupID:

$servicio[] = array('id'     => $produto->id
                   ,'name'   => $produto->name
                   ,'domain' => $produto->domain
                   ,'gid'    => $groupId);

This creates a new entry in the array, filling it with the new stuff you want. Functionally identical to the other examples of creating a temporary array, without having to actually create a temporary array.

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

Comments

0

You keep changing elements of $servico not adding elements on to it. Try doing something like this:

$newItem = Array(); // create the new item we want to add to $servico

// ...populate it like you have been...
$newItem ['id'] = $produto->id;
$newItem ['name'] = $produto->name;
$newItem ['domain'] = $produto->domain;

//seta o pid do produto
$servicoVo->setPid($produto->pid);

//vai buscar o gid do produto:
$groupId = $servicoDao->grupoServico($servicoVo);

//adiciona o group id ao array
$newItem ['gid'] = $groupId;

$servico[] = $newItem; // <-- note this change here, we add the new item to the array

Comments

0

If you want your script to store all iterations and not just the last one, you'll want to modify your script's foreach nested loop to do something similar to this:

foreach ($resposta->products as $produtos)
{
    foreach ($produtos as $produto)
    {

        if (!empty($produto->domain))
        {
            // Create a temporary array
            $newServico = array();

            $newServico['id'] = $produto->id;
            $newServico['name'] = $produto->name;
            $newServico['domain'] = $produto->domain;

            //seta o pid do produto
            $servicoVo->setPid($produto->pid);

            //vai buscar o gid do produto:
            $groupId = $servicoDao->grupoServico($servicoVo);

            //adiciona o group id ao array
            $newServico['gid'] = $groupId;

            // Append the new array to the $servico main array
            $servico[] = $newServico;
            unset($newServico);
        }
    }
}

Hopefully that answers your question :)

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.