PHP arrays can store objects. Because PHP is a programming language with weak data types, arrays in PHP can store any number of data of any type, that is, there is no limit on the type of array elements, which can be numbers, strings, Boolean values, arrays, Object objects, etc. .
The operating environment of this tutorial: Windows 10 system, PHP8.1.3 version, Dell G3 computer.
Array is one of the most important data types in PHP and is widely used in PHP. Because PHP is a programming language with weak data types, array variables in PHP can store any number of data of any type, and can implement the functions of data structures such as heaps, stacks, and queues in other strong data types.
Simply put, there is no restriction on the type of PHP array elements, which can be numbers, strings, Boolean values, arrays, Object objects, etc.
Example 1:
<?php header("Content-type:text/html;charset=utf-8"); $arr= array(1,2,"3",4,"hello",TRUE); var_dump($arr); ?>
Example 2:
<?php header("Content-type:text/html;charset=utf-8"); $array = array ( array("张三",25,"男"), array("李四",21,"男"), array("娜娜",22,"女") ); var_dump($array); ?>
If the elements in an array is another array, which constitutes an array containing arrays, that is, a multi-dimensional array:
Two-dimensional array
Three-dimensional array
Four-dimensional array
. ....
However, when the array exceeds three dimensions, its readability will be greatly reduced, and it will also be inconvenient to manage.
Example 3:
<?php header('content-type:text/html;charset=utf-8'); class foo{ function do_foo(){ echo "Doing foo."; } } $bar = new foo; $arry1=array(1,$bar);//这里将实例化的对象存入数组 var_dump($arry1);//这里打印数组结构 你会发现下标1的位置存储了一个object对象 $arry1[1]->do_foo();//以数组形式 调用do_foo(); $bar->do_foo();//正常的调用do_foo() //两种方式输出是一样的 充分说明 数组是可以存储对象的 ?>
The above is the detailed content of Can php arrays store objects?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version

WebStorm Mac version
Useful JavaScript development tools
