Help with ARRAY

I am pretty new to php and am working on a project.

In the simplest form, for the time being, I need to:
make a blank array,
then make a class that holds an object with some properties
then push that class and it’s properties to the array.

Is this a thing? Is this possible?
For example, I have multiple events I need listed out with their title, time, date, and presenter. When I print the array, it should show all events with their information listed.
I’m just having problems figuring out how to put this all together.

Here is what I have going so far:

[php]$events = array();

class Tours {
public $title = ‘Guided Tours’;
public $date = “Tuesday”;
public $time = “1:00 pm”;
public $presenter = “Gallery Guides”;
}

class Medit {
public $title = ‘Mindful Meditation’;
public $date = “Wednesday”;
public $time = “12:30 pm”;
public $presenter = “Galleries”;
}

class Kids {
public $title = ‘Storytelling & Art’;
public $date = “First Fridays”;
public $time = “10:30 am”;
public $presenter = “Galleries”;
}

$tours = new Tours;
$medit = new Medit;
$kids = new Kids;

array_push(‘Tours’, ‘Medit’, ‘Kids’);
print $events;
?>[/php]

It might be a total disaster, but any help would be appreciate!
Here is the error message I am getting:
Warning: array_push() expects parameter 1 to be array, object given in /home/angelazer/angelaivycreative.com/php_practice/index.php on line 36
Array

Your classes are incorrect. array_push needs to know what array to push the element into. Take this example:

[php]class Person
{
private $_name;
private $_age;

public function __construct( $n, $age ) {
    $this->name = $n;
    if( !filter_var( $age, FILTER_VALIDATE_INT))
        $this->_age = 0;
    else
        $this->_age = $age;
}

public function getName()
{
    return $this->name;
}

public function getAge()
{
    return $this->_age;
}

public function __toString()
{
    return "{$this->getName()} is {$this->getAge()}";
}

}

$person[0] = new Person( ‘Jon’, 47);
$person[1] = new Person( ‘James’, 26);
$person[2] = new Person( ‘Lynne’, 26);

$people = array();

foreach ( $person as $p )
{
array_push($people, $p);
}

echo “

”;
print_r( $people );
echo “
”;[/php]

Here’s another way to go about it:

[php]<?php

class MyRecord {

private $title = \NULL;
private $day = \NULL;
private $time = \NULL;
private $presenter = \NULL;
private $temp = [];
public $events = [];

public function __construct($title, $day, $time, $presenter) {
    $this->title = $title;
    $this->day = $day;
    $this->time = $time;
    $this->presenter = $presenter;
    $this->process();
}

public function add($title, $day, $time, $presenter) {
    self::__construct($title, $day, $time, $presenter);
}

private function process() {
    $this->temp['title'] = $this->title;
    $this->temp['day'] = $this->day;
    $this->temp['time'] = $this->time;
    $this->temp['presenter'] = $this->presenter;
    array_push($this->events, $this->temp);
}

public function myOutput() {
    echo '<pre>' . print_r($this->events, 1) . '</pre>';
}

}

$process = new MyRecord(‘Guided Tours’, ‘Tuesday’, ‘1:00 pm’, ‘Gallery Guides’);
$process->add(‘Mindful Medition’, ‘Wednesday’, ‘12:30 pm’, ‘Galleries’);
$process->add(‘Storytelling & Art’, “First Fridays”, “10:30 am”, “Galleries”);

$process->myOutput();
[/php]

Though this is sloppy class writing, if I took my time I would either use abstract classes or Dependency Injection instead of using static method.

Thank you for the input! Will try this as well.
I also tried to use a 2 dimensional array which may work for the project also .

<?php $events = array( array( Title => "Guided Tours", Date => "Tuesday", Time => "1 pm", ), array( Title => "Mindful Meditation", Date => "Wednesday", Time => "12:30 pm", ), array( Title => "Art+Storytelling", Date => "First Fridays", Time => "10:30 am" ) ); for ($row = 0; $row < 3; $row++) { echo $events[$row]["Title"]."
".$events[$row]["Date"]."
".$events[$row]["Time"]; echo "
"; echo "
"; } ?>

You basically have two dimensional arrays with the top two examples anyways (I know for sure with mine).

[php]<?php

class MyRecord {

private $title = \NULL;
private $day = \NULL;
private $time = \NULL;
private $presenter = \NULL;
private $temp = [];
public $events = [];

public function __construct($title, $day, $time, $presenter) {
    $this->title = $title;
    $this->day = $day;
    $this->time = $time;
    $this->presenter = $presenter;
    $this->process();
}

public function add($title, $day, $time, $presenter) {
    self::__construct($title, $day, $time, $presenter);
}

private function process() {
    $this->temp['title'] = $this->title;
    $this->temp['day'] = $this->day;
    $this->temp['time'] = $this->time;
    $this->temp['presenter'] = $this->presenter;
    array_push($this->events, $this->temp);
}

public function myOutput() {
    foreach ($this->events as $event) {
        echo "<p>Title: " . $event['title']. "<br>Day: " . $event['day']. "<br>Time: " . $event['time'] . "<br>Presenter: " . $event['presenter'] . "</p>\n";
    }
}

}

$process = new MyRecord(‘Guided Tours’, ‘Tuesday’, ‘1:00 pm’, ‘Gallery Guides’);
$process->add(‘Mindful Medition’, ‘Wednesday’, ‘12:30 pm’, ‘Galleries’);
$process->add(‘Storytelling & Art’, “First Fridays”, “10:30 am”, “Galleries”);

$process->myOutput();

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service