Get record from an array

I am trying to get a item out of an array rather than looping through each item.

I am able to do:

foreach($model->questions as $item){…};

but what if I just want one item at a time, somthing like:

$item = current($model->questions);

Is there a way to do this?

Thanks, Joe

Do you search for the item by the key or by the value?

Take a look at the array functions in the PHP Manual

Here is what I got working,

In my TESTS Controller:




	public function actiontestQuestions($id)

	{

		$this->render('testQuestions',array(

			'model'=>$this->loadModel($id),

		));

	}



And in my View:




<?

echo $model->TEST_NAME;


echo "<br /><br />";

foreach($model->questions as $item){

	echo "<h1>";

	echo $item->QUESTION; 

	echo "</h1>";

	echo "<form>";

	foreach($item->answers as $ans){		

		echo "<input type='radio' name='sex' value='male' />".$ans->ANSWER."</input>";

		echo "<br />";

		}

	echo "</form>";

	echo "<br />";

}


?>



It outputs

Test Name

This is question 1

This is answer 1 for question 1

This is answer 2 for question 1

This is answer 3 for question 1

This is question 2

This is answer 1 for question 2

This is answer 2 for question 2

But, I just want to show one question with answers at a time, Im not sure if im going about it wrong…

And why do you load multiple questions from db, if you only want to display one?

I would create a controller action for a single question:





  public function actiontestQuestion($catalogId,$questionId)

        {

                $this->render('testQuestion',array(

                        'model'=>$this->loadQuestion($catalogId,$questionId),

                ));

        }