Foreach need help?

$userIdList is an array that contains users ids!

I can’t make this foreach to loop though all of them! It only gets the first id?

foreach ($userIdList as $userId)        
{
$questions = BOL_UserService::getInstance()->getUserViewQuestions($userId);

if ( !empty($questions['data'][$userId]) )
        {
            $data = array();
            $test= array();
            foreach ( $questions['data'][$userId] as $key => $value )
            {
                if ( is_array($value) )
                {
                    $questions['data'][$userId][$key] = implode(', ', $value);
                    $test[] = $questions['data'];
                }
            }
        }
}
print_r($test);

This seem to work but it duplicates all the results!

$test= array();            
foreach ($userIdList as $userId)        
{
$questions = BOL_UserService::getInstance()->getUserViewQuestions($userId);

if ( !empty($questions['data'][$userId]) )
        {
            $data = array();

            foreach ( $questions['data'][$userId] as $key => $value )
            {
                if ( is_array($value) )
                {
                    $questions['data'][$userId][$key] = implode(', ', $value);
                    $test[] = $questions['data'];
                }
            }
        }
}
print_r($test);

Hard to comment without seeing what comes back in $questions.

Should the assignment to $test only occur if $value is an array, or should that line happen regardless, and the implode() only happen in that case?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.