1

I am new to codeigniter.

when need to display 5th element value of an array in java normally we use,

System.out.print(array[4]);

in controller of codeigniter I use,

$data['test'] = $this->Model_test->getValues();

to assign value into the array. and then I use,

$this->load->view('test_view', $data);

to load view.

inside view I use,

foreach ($questions as $object) {
    echo $object->question;
}

to display all values. If I need to display only the value of 5th element, what should I do?

2
  • 1
    Is it an array or an array of object if its an array you can simply use $questions[5] for array of object $questions->5 Commented Nov 13, 2015 at 5:55
  • 1
    If $object is an array then you could simply use $object[4] which will print the fifth index position value Commented Nov 13, 2015 at 5:55

2 Answers 2

1

Try this,

foreach ($questions as $object) {
    echo $object->question;
}

Above will echo each row's question. to get only 5th row, check this.

echo $questions[5]->question;
Sign up to request clarification or add additional context in comments.

2 Comments

...wait, if $object is an object (stated when he showed us $object->question), how do you plan on using array subscript to access a property?
0

Try this

print_r($questions[4]); # This will print fifth element of an array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.