Any of the following are valid:

$myArray = array();
$myArray[] = new Object();
$myArray[1] = new Object();
array_push($myArray, new Object);
Answer from Byron Whitlock on Stack Overflow
🌐
Delft Stack
delftstack.com › home › howto › php › php array of objects
How to Create Array of Objects in PHP | Delft Stack
February 2, 2024 - Python ScipyPythonPython TkinterBatchPowerShellPython PandasNumpyPython FlaskDjangoMatplotlibDockerPlotlySeabornMatlabLinuxGitCCppHTMLJavaScriptjQueryPython PygameTensorFlowTypeScriptAngularReactCSSPHPJavaGoKotlinNode.jsCsharpRustRubyArduinoMySQLMongoDBPostgresSQLiteRVBAScalaRaspberry Pi ... This article will introduce methods to create an array of objects in PHP...
Top answer
1 of 4
30

All of these are quite obvious really. I'm only listing the Pythonic ways to do these things. Update: Examples should now work in both Python 2 and Python 3. In Python 2 you could substitute xrange() for range() and iteritems() for items() for efficiency.

Example 1

PHP

$php = array(1,2,3,4,5,6,7,8,9,10);
for (i < 10 ; $i ++)
echo i]."<br>";

Python (generally you iterate over lists in Python, instead of accessing by index):

lst = [1,2,3,4,5,6,7,8,9,10]
for item in lst:
    print(str(item) + "<br>")

Example 2

for (i < 10 ; $i ++)
echo i] = $i +1 ;

Python:

lst = range(1, 11)
for item in lst:
    print(item)

Or maybe:

lst = []
for i in range(10):
    lst.append(i + 1)
    print(lst[-1])     # prints out last element

Example 3

$php = array(1,2,3,4,5,6,7,8,9,10);
foreach (value)
echo $value."<br>";

Same as 1st

Example 4

$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}

Python:

lst = [1, 2, 3, 4]
lst = [val * 2 for val in lst]

Example 5

$arr = array("mot"=>"one", "hai"=>"two","ba"=> "three");
foreach (key => $value) {
    echo "Key: $key; Value: $value<br />\n";
}

Python (note that {...} creates a dict [dictionary] in Python, not a list/):

dct = {'mot': 'one', 'hai': 'two', 'ba': 'three'}
for key, value in dct.items():
    print("Key: %s; Value: %s<br />" % (key, value))

Example 6

$arr = array("one", "two","three");
while (list(value) = each($arr)) {
    echo "Key: $key; Value: $value<br />\n";
}

Python:

lst = ['one', 'two', 'three']
for key, value in enumerate(lst):
    print("Key: %d; Value: %s<br />" % (key, value))

Example 7

$arr = array("one", "two","three");
while ($element = each($arr)) {
    echo "Key: $element['key']; Value: $element['value']<br />\n";
}

There is no direct Python equivalent to this.

Example 8

$products = array( array("ITL","INTEL","HARD"),
                        array("MIR", "MICROSOFT","SOFT"),
                        array("Py4C", "pythonkhmer.wordpress.com","TUTORIAL")
                         );
for (row < 3; $row++)
{
    for ($col = 0; col++)
    {
        echo "|".$products[col];
    }
    echo "<br>";
}

Python:

products = [['ITL', 'INTEL', 'HARD'],
    ['MIR', 'MICROSOFT', 'SOFT'],
    ['Py4C', 'pythonkhmer.wordpress.com', 'TUTORIAL']]

for product in products:
    for item in product:
        print('|' + item)
    print('<br>')

Or maybe a more Pythonic version:

for product in products:
    print('|%s<br>' % ('|'.join(product)))
2 of 4
10

"What is in Python?", quite a philosophical question, always a great way to start a day. I think what is in Python can best be answered by the Zen of Python (type import this in an interactive shell):

  • Beautiful is better than ugly.
  • Explicit is better than implicit.
  • Simple is better than complex.
  • Complex is better than complicated.
  • Flat is better than nested.
  • Sparse is better than dense.
  • Readability counts.
  • Special cases aren't special enough to break the rules.
  • Although practicality beats purity.
  • Errors should never pass silently.
  • Unless explicitly silenced.
  • In the face of ambiguity, refuse the temptation to guess.
  • There should be one-- and preferably only one --obvious way to do it.
  • Although that way may not be obvious at first unless you're Dutch.
  • Now is better than never.
  • Although never is often better than right now.
  • If the implementation is hard to explain, it's a bad idea.
  • If the implementation is easy to explain, it may be a good idea.
  • Namespaces are one honking great idea -- let's do more of those!

Sorry, couldnt resist. To answer the question you meant to ask, I direct you to the Python documentation, specifically the section about looping techniques as linked to by lutz.

Unless the syntax in the documentation manage to completely confuse you (though I doubt it) you will see how a loop is defined in Python. And once you have understood that, you will understand, by definition, how they differ (syntactically) from the loops youre used to in PHP.

Still not satisfied? Hmm... I guess you should read the tutorial again. Then, come back and ask specific questions that could yield specific answers. You wont find any silver bullets for a question this broad.

🌐
W3Schools
w3schools.com › php › php_arrays.asp
PHP Arrays
The most common are strings and numbers, but array items can also be objects, functions or even arrays. You can have different data types in the same array. ... The real strength of PHP arrays are the built-in array functions.
🌐
TutorialsPoint
tutorialspoint.com › article › in-php-how-can-i-add-an-object-element-to-an-array
In PHP, how can I add an object element to an array?
March 15, 2026 - In PHP, you can add an object element to an array using several methods. The most common approach is to create an object and then append it to an array using the array push syntax.
🌐
PHP
php.net › manual › en › arrayobject.construct.php
PHP: ArrayObject::__construct - Manual
<?php $array = [ '1' => 'one', '2' => 'two', '3' => 'three' ]; $arrayobject = new ArrayObject($array); var_dump($arrayobject); ?> ... object(ArrayObject)#1 (1) { ["storage":"ArrayObject":private]=> array(3) { [1]=> string(3) "one" [2]=> string(3) ...
Find elsewhere
🌐
Honar Systems
honarsystems.com › home › php array object
PHP Array Object - Honar Systems
August 10, 2025 - PHP array object allows object to work as array. Array does not object in PHP but PHP gives some ways to make objects that act like arrays.
🌐
Laracasts
laracasts.com › discuss › channels › laravel › php-create-array-of-object-with-array
php - create array of object with array
$exam_data = tbl_exam_data::join('tbl_subjects', 'tbl_subjects.subject_id', 'exam_data_subject_id') ->where('exam_data_exam_id', $exam_id)->get([ 'exam_data_id', 'subject_name', 'subject_id', 'has_negative_marking', 'negative_marks' ]); $exam_details = tbl_exam::find($exam_id); $usersData = User::where('user_id', 2)->where("stud_class", $exam_details->exam_class_id)->where("stud_sub_branch", $exam_details->exam_branch_id)->get(['name', 'id']); $studentsArray = []; foreach ($usersData as $user) { $studentsArray = array( "student" => $user->name, "subjects" => [] ); foreach ($exam_data as $subje
🌐
Stack Overflow
stackoverflow.com › questions › 51281447 › create-an-array-of-object-php
Create an array of object PHP - Stack Overflow
$structure = new stdClass(); $structure->description = 'tobedefined'; // Define the description here $structure->deposit = getPrices($id); function getPrices($id) { $deposits = array(); // Change this to contain the real data // $test = Prices::where('price_id',$id)->where('promo',0)->get(); $test = array( array( 'item_id' => 100, 'price' => '1, 2, 3, 4, 5', 'type' => 'child' ), array( 'item_id' => 101, 'price' => '2, 4, 6, 8, 10', 'type' => 'child' ), array( 'item_id' => 102, 'price' => '2, 4, 6, 8, 10', 'type' => 'child' ) ); foreach ($test as $t) { $deposit = new stdClass(); $deposit->item_
🌐
W3Schools
w3schools.com › python › python_arrays.asp
Python Arrays
Arrays are used to store multiple values in one single variable.
🌐
YouTube
youtube.com › watch
PHP Array To Object - YouTube
Want more? Explore the library at https://www.codecourse.com/lessonsOfficial sitehttps://www.codecourse.comTwitterhttps://twitter.com/teamcodecourse
Published: May 21, 2014
🌐
W3Schools
w3schools.com › php › func_array.asp
PHP array() Function
Create an indexed array named $cars, assign three elements to it, and then print a text containing the array values.
🌐
DEV Community
dev.to › yanyy › create-an-array-object-and-access-array-elements-in-php-ibo
Create an array and access array elements in PHP - DEV Community
April 18, 2022 - In PHP, we can create indexed and associative arrays, where integer and string can be used as keys respectively.
🌐
IONOS UK
ionos.co.uk › digital guide › websites › web development › php arrays
How to use a PHP array - IONOS UK
October 28, 2024 - The ArrayList data structure is ... manage objects in lists. Unlike arrays, Java ArrayList is able to resize dy­nam­ic­ally and comes with functions for adding, removing, sorting and searching elements. In this… ... If you want to bundle values and items in a container, you can use arrays for this in most cases. Although arrays in Python are not common, there is a good al­tern­at­ive in the language. Using lists, you have the option to create arrays in ...
🌐
Java2Blog
java2blog.com › create-array-of-objects-php
Create Array of Objects in PHP – Java2Blog
December 4, 2022 - Then created multiple objects and used array() to create array of objects in PHP.