Welcome Guest, Not a member yet? Register   Sign In
echo value in an array inside an array
#1

[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
  'id' => string '4' (length=1)
  'address' => string '22 Acacia Ave' (length=20)
  'status' => string 'Conveyancing' (length=12)

So I thought i could access the variable in the view like this
Code:
echo $value->$key->id
but that didnt work, I have tried
Code:
echo $value->$key['id']
and that didnt work either I keep getting

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

#2

[eluser]Tpojka[/eluser]
If it is an array, maybe you should approach it with
Code:
$value['$id']
#3

[eluser]kivison[/eluser]
Thanks for getting back

That produces the following error

Fatal error: Cannot use object of type stdClass as array
#4

[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?
#5

[eluser]kivison[/eluser]
Thanks for getting back

it is an array. The dump of $conveyancing is

Code:
object(stdClass)[19]
  public '4' =>
    array
      'id' => string '4' (length=1)
      'address' => string 'Agincourt - NE31 6RD' (length=20)
      'status' => string 'Conveyancing' (length=12)

and the dump of $conveyancing as a 'foreach as $key=>$value is
Code:
array
  'id' => string '4' (length=1)
  'address' => string '22 Acacia Ave' (length=20)
  'status' => string 'Conveyancing' (length=12)

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
#6

[eluser]Tpojka[/eluser]
First code seems to be an object as writen.
Try with:
Code:
(foreach $conveyancing->public as $key => $value)
Not sure if that solve your issue.

Here you can get Object Iteration.
#7

[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){

Sad

Keith
#8

[eluser]CroNiX[/eluser]
Does this work?
Code:
foreach($conveyancing as $value)
{
  echo $value['id'] . '<br>';
}
or
Code:
foreach($conveyancing as $ar)
{
  foreach($ar as $key => $value)
  {
    echo "$key::$value<br>";
  }
}
#9

[eluser]kivison[/eluser]
You are a genius

seemingly the only way I can do this is the following
Code:
foreach($conveyancing as $key => $value){
  foreach($value as $info){

which seems strange that I cant access the variable straight

Thank you dude. Respect!!!!!

Keith
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB