echo value in an array inside an array |
[eluser]kivison[/eluser]
Hi Gurus This is driving me nuts. I am pretty new to CI, I really like it but I am hitting some pretty solid brick walls. My current problem is as follows. presented an array to the view and accessed it like this Code: foreach($conveyancing as $key => $value){ When I var_dump the $value->$key like this Code: var_dump($value->$key I get the following so I know that in the $value->$key variable there is an array with 'id','address' and 'status in there. Code: array So I thought i could access the variable in the view like this Code: echo $value->$key->id Code: echo $value->$key['id'] A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: views/portfolio.php Line Number: 59 Can someone please help me before I loose all my hair and nails Thanks in advance Keith
[eluser]Tpojka[/eluser]
If it is an array, maybe you should approach it with Code: $value['$id']
[eluser]kivison[/eluser]
Thanks for getting back That produces the following error Fatal error: Cannot use object of type stdClass as array
[eluser]Tpojka[/eluser]
Can you bit more describe $conveyancing? What are those values? How do you get it? Is that an object or an array?
[eluser]kivison[/eluser]
Thanks for getting back it is an array. The dump of $conveyancing is Code: object(stdClass)[19] and the dump of $conveyancing as a 'foreach as $key=>$value is Code: array so i need to be able to display $conveyancing (array) to -> $key (which is an array)-> the key value of the resultant array in short the value in an array in an array Its a proper head scratcher :S Keith
[eluser]Tpojka[/eluser]
First code seems to be an object as writen. Try with: Code: (foreach $conveyancing->public as $key => $value) Here you can get Object Iteration.
[eluser]kivison[/eluser]
Hi Returns A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: views/portfolio.php Line Number: 55 This line is foreach($conveyancing->public as $key => $value){ ![]() Keith
[eluser]CroNiX[/eluser]
Does this work? Code: foreach($conveyancing as $value) Code: foreach($conveyancing as $ar)
[eluser]kivison[/eluser]
You are a genius seemingly the only way I can do this is the following Code: foreach($conveyancing as $key => $value){ which seems strange that I cant access the variable straight Thank you dude. Respect!!!!! Keith
[eluser]CroNiX[/eluser]
It's because it's not just an array. It's an array of objects, and each object contains an array. I'm guessing $conveyancing is the db::result() of a db query which only returned one row. If it returned multiple rows it might make more sense. If this query is always going to return only one row, I'd use $this->db->row() or $this->db->row_array() instead. Then you won't have an outer array and can directly access it. |
Welcome Guest, Not a member yet? Register Sign In |