Alex Miles
Alex Miles

Reputation: 151

How to create array of objects in php

I'm attempting to create an array of objects in php and was curious how I would go about that. Any help would be great, thanks!

Here is the class that will be contained in the array

<?php

class hoteldetails {
private $hotelinfo;
private $price;

public function sethotelinfo($hotelinfo){
    $this->hotelinfo=$hotelinfo;
}

public function setprice($price){
    $this->price=$price;
}

public function gethotelinfo(){
    return $hotelinfo;
}

public function getprice(){
    return $price;
}

}

And here is what I am attempting to do-

<?PHP
include 'file.php';

$hotelsdetail=array();    

$hotelsdetail[0]=new hoteldetails();
$hotelsdetail[0].sethotelinfo($rs);
$hotelsdetail[0].setprice('150');


?>

The class attempting to create the array doesn't compile but is just a best guess as to how I can do this. Thanks again

Upvotes: 8

Views: 48078

Answers (3)

Bhupendra Jha
Bhupendra Jha

Reputation: 37

You can get the array of object by encoding it into json and decoding it with $assoc flag as FALSE in json_decode() function.

See the following example:

    $attachment_ids = array();
    $attachment_ids[0]['attach_id'] = 'test';
    $attachment_ids[1]['attach_id'] = 'test1';
    $attachment_ids[2]['attach_id'] = 'test2';
    $attachment_ids                 = json_encode($attachment_ids);
    $attachment_ids                 = json_decode($attachment_ids, FALSE);
    print_r($attachment_ids);

It would render an array of objects.

output:

Array
(
[0] => stdClass Object
    (
        [attach_id] => test
    )

[1] => stdClass Object
    (
        [attach_id] => test1
    )

[2] => stdClass Object
    (
        [attach_id] => test2
    )

)

Upvotes: 0

slhck
slhck

Reputation: 38682

What you should probably do is:

$hotelsDetail = array();

$details = new HotelDetails();
$details->setHotelInfo($rs);
$details->setPrice('150');

// assign it to the array here; you don't need the [0] index then
$hotelsDetail[] = $details;

In your specific case, the issue is that you should use ->, not .. The period isn't used in PHP to access attributes or methods of a class:

$hotelsdetail[0] = new hoteldetails();
$hotelsdetail[0]->sethotelinfo($rs);
$hotelsdetail[0]->setprice('150'); 

Note that I capitalized the class, object, and function names properly. Writing everything in lowercase is not considered good style.

As a side note, why is your price a string? It should be a number, really, if you ever want to do proper calculations with it.

Upvotes: 20

Bryant Makes Programs
Bryant Makes Programs

Reputation: 1694

You should append to your array, not assign to index zero.

$hotelsdetail = array();    
$hotelsdetail[] = new hoteldetails();

This will append the object to the end of the array.

$hotelsdetail = array();    
$hotelsdetail[] = new hoteldetails();
$hotelsdetail[] = new hoteldetails();
$hotelsdetail[] = new hoteldetails();

This would create an array with three objects, appending each one successively.


Additionally, to correctly access an objects properties, you should use the -> operator.

$hotelsdetail[0]->sethotelinfo($rs);
$hotelsdetail[0]->setprice('150');

Upvotes: 2

Related Questions