1

I am pulling elements from a standard class object into an assoc array like so:

$array = $subjects;
foreach ( $array as $subject ) {

    foreach ( $subject as $prop => $val ) {
        if ( $val !== '' ) {
         echo $prop . ' = ' . $val;
         echo "<br>";
        }
    }
}

I get the result I expect from above, except what I'd like to do is echo out individual values into a table.

When I do this: echo $subject['day1']; I get this: "Cannot use object of type stdClass as array."

Where am I going wrong? Thanks in advance.

Vegemike
  • 37
  • 7
  • You don't have an array, you have a `stdClass`. Try http://php.net/manual/en/function.get-object-vars.php – Isaac Dec 08 '16 at 20:29
  • 2
    Possible duplicate of [Cannot use object of type stdClass as array?](http://stackoverflow.com/questions/6815520/cannot-use-object-of-type-stdclass-as-array) –  Dec 08 '16 at 20:30

2 Answers2

1

If it's using StdClass you'll need to do this:

$subject->day1

If you want to convert it to an array, have a look at this question: php stdClass to array

Community
  • 1
  • 1
Magd Kudama
  • 3,229
  • 2
  • 21
  • 25
0

You are trying to iterate over the $array and $array is still an object. Use this:

    $vars = get_object_vars($subjects);

to get assoc. array from the object $subjects. Then go:

foreach ($vars as $name => $value) {
    echo $name . "=" . $value;
    echo "<br>";
}
Vladimir Despotovic
  • 3,200
  • 2
  • 30
  • 58
  • 1
    get_object _vars gives an array of 0. – Vegemike Dec 08 '16 at 20:42
  • 1
    Then your object is not well defined. Tell me what do you get if you type this: print "
    "; print_r($subjects); print "
    ";
    – Vladimir Despotovic Dec 08 '16 at 20:43
  • Array ( [0] => stdClass Object ( [T_id] => 3 [id] => 27 [day11] => Maths [day12] => English [day13] => Free Period [day14] => Chemistry [day15] => Physics [day16] => IPT [day21] => Chemistry [day22] => Chemistry [day23] => Maths [day24] => English [day25] => Free Period [day26] => Physics – Vegemike Dec 08 '16 at 20:46
  • 1
    That means that $subjects is already an array, not an object. – Vladimir Despotovic Dec 08 '16 at 20:47
  • That's what I get. What would be an efficient way to access any individual values? That's what is messing me up. – Vegemike Dec 08 '16 at 20:47
  • 1
    Ok, use this: $vars = get_object_vars($subjects[0]); to get assoc. array from the array $subjects first element. Then go: foreach ($vars as $name => $value) { echo $name . "=" . $value; echo "
    "; }
    – Vladimir Despotovic Dec 08 '16 at 20:49
  • 1
    when I do this: echo $subject->day11; I get expected result. I want to put all values in a table. How to loop through values? If i add an index like this: $x ='11'; echo $subject->day.$x; $x++; will not work. What I want is efficient way to get to each value. Driving me mad. – Vegemike Dec 08 '16 at 20:54
  • 1
    If you already have $subject that you can do $subject -> day11 with, then do this: foreach (get_object_vars($subject) as $day => $subject_name) { echo "day: " . $day . ", subject name: " . $subject_name; } – Vladimir Despotovic Dec 08 '16 at 20:59
  • 1
    OK i have done that and now have a clean looking array. – Vegemike Dec 08 '16 at 21:00
  • 1
    Ok, please upvote my comments and accept my answer and upvote my answer if you are satisfied with it. Thanks. – Vladimir Despotovic Dec 08 '16 at 21:01