• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Access elements in an object array using an index - PHP

Graze

Senior member
eg.
Code:
class Person
{

public $Height;
public $Age;

}


$firstPerson = new Person();
$firstPerson->Height = 136;
$firstPerson->Age = 26;

$secondPerson = new Person();
$secondPerson->Height = 124;
$secondPerson->Age = 18;


Persons = array($firstPerson, $secondPerson);


I have an array of objects that I would like to access using an index syntax instead of having to use the foreach loop in php.


I cant seem to use a normal index like Person[0]->Height to access the Height property of the person at position 0 in the array. In C# it would have been so simple as Person[0].Height

Am I doing something wrong or is there anyway I could do something similar?
 
Perhaps you meant $Persons?

Also, just from a style perspective, I find your capitalization to be very inconsistent.
 
Perhaps you meant $Persons?

Also, just from a style perspective, I find your capitalization to be very inconsistent.

LOL, yes, I meant $Persons. This is not what my code looks like but an example of what I want to do that I quickly typed up for the thread.

I use PascalCase for non local variables and methods names and camelCase for local one.
 
Well, I plugged this into my local Drupal install:

PHP:
<?php
class Person
{

public $Height;
public $Age;

}


$firstPerson = new Person();
$firstPerson->Height = 136;
$firstPerson->Age = 26;

$secondPerson = new Person();
$secondPerson->Height = 124;
$secondPerson->Age = 18;


$persons = array($firstPerson, $secondPerson);
print_r($persons);
print "The first person's height is ".$persons[0]->Height;
?>

And I got this:

Code:
Array ( [0] => Person Object ( [Height] => 136 [Age] => 26 ) [1] => Person Object ( [Height] => 124 [Age] => 18 ) ) The first person's height is 136

Looks right to me. 🙂
 
Well, I plugged this into my local Drupal install:

PHP:
<?php
class Person
{

public $Height;
public $Age;

}


$firstPerson = new Person();
$firstPerson->Height = 136;
$firstPerson->Age = 26;

$secondPerson = new Person();
$secondPerson->Height = 124;
$secondPerson->Age = 18;


$persons = array($firstPerson, $secondPerson);
print_r($persons);
print "The first person's height is ".$persons[0]->Height;
?>

And I got this:

Code:
Array ( [0] => Person Object ( [Height] => 136 [Age] => 26 ) [1] => Person Object ( [Height] => 124 [Age] => 18 ) ) The first person's height is 136

Looks right to me. 🙂


Thanks buddy. Knowing it was possible I looked over my code and used the print_r and read thought my messy results.

Turns out I misspelt a property while assigning while assigning a value to it using a loop.
Long story short the first few object properties in the array were messed up and were left blank. When I used the index to access these the would just return nothing so I assumed it wasn't working.

The joys of a dynamic language is that I don't see these things before hand.
 
Back
Top