luftikus143
luftikus143

Reputation: 1285

How to use a PHP object as an array

I have a heard time figuring out how to use a class as an array. This response helped me a bit on the way; but it doesn't work.

I have a class like this one:

class body_text
{
    // class body_text's attributes
    var $position;
    var $content;
}

And I want to use it as an array:

$body_text_id = 0; 

while ($row['page'] <> "")
{           
    $body_text[$body_text_id] -> position = trim($row["page"]);
    $body_text[$body_text_id] -> content = trim($row["text");
    $body_text_id++;
}

Now, it seems I would need to use this for my class

class body_text_class extends ArrayObject

and use this then:

$body_text = new body_text_class(); 

But that doesn't work.

I am sure there is a simple answer to this... but I can't find it. Thanks for any hints!

EDIT: Added another line in the loop to make it clearer. Sorry! My aim: I get back many bits of text and its position within a website from a first query ("$row['page']"), and I want to store these in a variable for later use.

Upvotes: 0

Views: 97

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

Hard to follow the code but you are just showing an array of objects:

for($i=0; $i<10; $i++) {
    $body_text[$i] = new body_text;
    $body_text[$i]->position = 'something';
}
print_r($body_text);

Upvotes: 2

Related Questions