1

In my controller i'm using data variable like this

$data['some_var'] = $this->Model->some_info($param);

So if i do var_dump this $data['some_var'] it will print like this

  object(StdClass)[4]
  Public 'blah1' => String 'blah1' (Length=5)
  Public 'blah2' => String 'blah2' (Length=5)
  Public 'blah3' => String 'blah3' (Length=5)
  Public 'blah4' => String 'blah4' (Length=5)

I know how to use this $data[] into view file but i want to use this $data[] into controller file,

What i want , i want to use blah1 from array

I tried like this

$var = $data['some_var']['blah1'];

$var = $some_var->blah1;

I'm not sure how to sort-out this things

2
  • can you show var_export() result instead var_dump() so that we can try Commented Jun 11, 2016 at 11:00
  • array ( 0 => stdClass::__set_state(array( 'blah1' => 'blah1', 'blah2' => 'blah2', )), ) Nothing Different , there has to be way but i don't know how Commented Jun 11, 2016 at 11:05

3 Answers 3

1

ok try this it will work

$var = $data['some_var'][0]->blah1;
Sign up to request clarification or add additional context in comments.

Comments

0

Try

$var = $data['some_var']->blah1;

1 Comment

This won't working , this has to be way but i don't know how @Rana Soyab
0

you might want to try the solution of typecasting your object - answer from Gordon : details here

// typecast your object
$data['some_var'] = (array) $this->Model->some_info($param);

// access the index of the array
// !! this won't work if ['blah1'] contains an Object !!
$var = $data['some_var']['blah1'];

If you want to access sub-object as well I guess you'll have to write a recursive function that will typecast every level see Gordon's answer as well

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.