0

Morning everyone,

My problem seems to be in my foreach loop but I cant see the issue.

When I print my array I get this from my select statment.

array(41) {
  [0]=> array(4) {
    ["id"]=>
    string(1) "1"
    ["name"]=>
    string(14) "Indoor Cycling"
    ["time"]=>
    string(12) "6.15am – 7am"
    ["day"]=>
    string(1) "1"
  }
  [1]=> array(4) {
    ["id"]=>
    string(2) "73"
    ["name"]=>
    string(11) "Fast Blast "
    ["time"]=>
    string(6) "7.10am"
    ["day"]=>
    string(1) "5"
  }
}

So I try the standard for each loop.

foreach ($rows as $timetableitems) {
    $timetablearray[] = array(
        'name' => $timetableitems->name,
        'time'  => $timetableitems->time,
    );              
}

But when I try and var dump my $timetablearray I get the following.

array(41) {
  [0]=>
  array(2) {
    ["name"]=>
    NULL
    ["time"]=>
    NULL
  }
  [1]=>
  array(2) {
    ["name"]=>
    NULL
    ["time"]=>
    NULL
  }

Any help would be most appreciated, thanks.

1
  • You are calling the items like objects, not arrays? Commented Aug 20, 2013 at 10:50

3 Answers 3

5
foreach ($rows as $timetableitems) {
    $timetablearray[] = array(
        'name' => $timetableitems['name'],
        'time'  => $timetableitems['time'],
    );              
}

You can not access array variables by $timetableitems->name. You have to use $timetableitems['name']

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate sorry did not notice.
4

Instead of calling:

$timetableitems->name

use:

$timetableitems['name']

Because $timetableitems is an array, not an object. So you access its properties with key.

Comments

0

Try $timetableitems['name'] Instead of $timetableitems->name

$timetableitems is an array so you can use $timetableitems[] this format

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.