php中的数组列表 [英] ArrayList in php

查看:27
本文介绍了php中的数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

php 中除了array 之外还有其他数据结构吗?是否可以创建诸如 ArrayList 之类的数据结构?如果是这样,请提供一些参考或某种实现.

Are there any data structures in php other than array. Is it possible to create a data structure such as an ArrayList? If so please provide some references or some kind of implementation.

推荐答案

您需要了解的有关数组的所有信息都可以在 文档.

Everything you need to know about arrays can be found in the documentation.

所有可用的数组函数都列在函数参考.

All of the available functions for arrays are listed in the functions reference.

一些注意事项:

  • 数组最初是 PHP 中唯一的数据结构.这就是为什么它们如此灵活.它们旨在用作堆栈、队列、数组、列表、哈希表等.后来 PHP 引入了 spl 数据结构.
  • 与 Java 不同,PHP 不是纯 OO 语言.数组本身没有您可以应用的内置方法.这必须通过正常"函数来完成.
  • 数组没有固定大小.它们会自动扩展和收缩.

在下面我尝试列出最常见的 PHP 替代方案 ArrayList 方法:

In the following I tried to list the PHP alternatives for the most common ArrayList methods:

  • add(element) 基本上只是通过 $array[] = $element 追加.新值获取下一个空闲数字索引(这是首选方式).你也可以使用 array_push($array, $element).
  • addAll(ArrayList): array_merge($array1, $array2) 在某种程度上.合并关联数组时要小心.相同键的值将被覆盖.
  • clone():由于数组不是对象,您只需将数组分配给另一个变量即可克隆"它:

  • add(element) is basically just appending via $array[] = $element. The new value gets the next free numeric index (this is the preferred way). You can also use array_push($array, $element).
  • addAll(ArrayList): array_merge($array1, $array2) in a way. Be careful when merging associative arrays. Values for the same keys will be overwritten.
  • clone(): As arrays are not objects, you "clone" an array by just assigning it to another variable:

    $a = array(1,2,3);
    $b = $a;

  • contains(element):in_array($element, $array)

    这篇关于php中的数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆