php php中的数组列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4570266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ArrayList in php
提问by Cooper
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.
是否有任何数据结构php
以外array
。是否可以创建一个数据结构,例如ArrayList
?如果是这样,请提供一些参考或某种实现。
回答by Felix Kling
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.
函数参考中列出了数组的所有可用函数。
Some notes:
一些注意事项:
- Arrays were originally the only datastructure in PHP. That is why they are so flexible. They were meant to be used as stack, queue, array, list, hash table and so forth. Later on PHP introduced the spl Datastructures.
- Unlike Java, PHP is not a pure OO language. An array itself has no built in methods you can apply. This has to be done via "normal" functions.
- Arrays have no fixed size. They expand and shrink automatically.
- 数组最初是 PHP 中唯一的数据结构。这就是为什么它们如此灵活。它们旨在用作堆栈、队列、数组、列表、哈希表等。后来 PHP 引入了spl 数据结构。
- 与 Java 不同,PHP 不是纯 OO 语言。数组本身没有您可以应用的内置方法。这必须通过“正常”功能来完成。
- 数组没有固定大小。它们会自动扩展和收缩。
In the following I tried to list the PHP alternatives for the most common ArrayList
methods:
在下面,我尝试列出最常用ArrayList
方法的 PHP 替代方案:
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 usearray_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)
get(index)
: Like in most languages:$val = $array[index];
indexOf(element)
:array_keys($array, $element)
with the value you search for as second parameterisEmpty()
:empty($array)
remove(index)
: Itsunset($array[index])
remove(value)
with value: You have to get the keys first (seeindexOf
), iterate over they keys and useunset
.size()
:count($array)
add(element)
基本上只是附加通过$array[] = $element
. 新值获取下一个空闲数字索引(这是首选方式)。您也可以使用array_push($array, $element)
.addAll(ArrayList)
:array_merge($array1, $array2)
某种意义上。合并关联数组时要小心。相同键的值将被覆盖。clone()
:由于数组不是对象,您只需将数组分配给另一个变量即可“克隆”数组:$a = array(1,2,3); $b = $a;
contains(element)
:in_array($element, $array)
get(index)
:就像大多数语言一样:$val = $array[index];
indexOf(element)
:array_keys($array, $element)
使用您搜索的值作为第二个参数isEmpty()
:empty($array)
remove(index)
: 它的unset($array[index])
remove(value)
带值:您必须先获取密钥(请参阅indexOf
),遍历它们的密钥并使用unset
.size()
:count($array)
回答by Carlos H
I tried to implement, here's some simple code:
我尝试实现,这里有一些简单的代码:
class ArrayList {
private $list = array();
public function Add($obj)
{
array_push($this->list, $obj);
}
public function Remove($key)
{
if(array_key_exists($key, $this->list))
{
unset($this->list[$key]);
}
}
public function Size()
{
return count($this->list);
}
public function IsEmpty()
{
return empty($this->list);
}
public function GetObj($key)
{
if(array_key_exists($key, $this->list))
{
return $this->list[$key];
}
else
{
return NULL;
}
}
public function GetKey($obj)
{
$arrKeys = array_keys($this->list, $obj);
if(empty($arrKeys))
{
return -1;
}
else
{
return $arrKeys[0];
}
}
}
回答by Tom van der Woerdt
array(
'key' => 'value',
'key2' => 'value2'
)
or
或者
class DataStructure {
var $val1;
var $val2;
}
$item = new DataStructure();
$item -> val1 = 'value1';
I have no idea what this "arrayList" is (sounds Java), but if it's a list of arrays, the PHP equivalent would be :
我不知道这个“arrayList”是什么(听起来是 Java),但如果它是一个数组列表,那么 PHP 等效项将是:
array(
array(
'item1',
'item2'
),
array(
'item1',
'item2'
)
)
[edit]
[编辑]
If this arrayList
is simply an array, you could use
如果这arrayList
只是一个数组,则可以使用
array(
'item1',
'item2'
)
回答by Orwellophile
If you (or anyone else reading this) simply wants (for whatever reason) a Java like ArrayList for PHP, you can use this.
如果您(或其他阅读本文的人)只是想要(出于任何原因)像 ArrayList 这样的 Java for PHP,您可以使用它。
Full Source: http://www.phpclasses.org/browse/file/4191.html
完整来源:http: //www.phpclasses.org/browse/file/4191.html
/**
* ArrayList class
* @version 0.2
* @author Tim Anlauf <[email protected]>
* @url http://www.phpclasses.org/browse/file/4191.html
**/
class ArrayList {
function ArrayList($arr="")
function addToPos($index, $obj)
function add($obj)
function addAll($arr)
function clear()
function contains($obj)
function get($index)
function indexOf($obj)
function isEmpty()
function lastIndexOf($obj)
function remove($index)
function removeRange($fromIndex, $toIndex)
function size()
function sort()
function toArray()
function hasNext()
function reset()
function next()
function isInteger($toCheck)
}
回答by Romain Bessuges-Meusy
by extending the ArrayObject class, you might be able to create some basic class really easily (such as Vector, Collection of ArrayList).
通过扩展 ArrayObject 类,您可能能够非常轻松地创建一些基本类(例如 Vector、ArrayList 的集合)。
回答by Cypher
The closest PHP likeness to the ArrayListclass from Java is the ArrayObjectclass. The method names are different, but the functionality between the two is fairly close.
与 Java 中的ArrayList类最接近的 PHP类是ArrayObject类。方法名称不同,但两者之间的功能相当接近。
ArrayObject implements IteratorAggregate , ArrayAccess , Serializable , Countable {
/* Constants */
const integer STD_PROP_LIST = 1 ;
const integer ARRAY_AS_PROPS = 2 ;
/* Methods */
public __construct ([ mixed $input = [] [, int $flags = 0 [, string $iterator_class = "ArrayIterator" ]]] )
public void append ( mixed $value )
public void asort ( void )
public int count ( void )
public array exchangeArray ( mixed $input )
public array getArrayCopy ( void )
public int getFlags ( void )
public ArrayIterator getIterator ( void )
public string getIteratorClass ( void )
public void ksort ( void )
public void natcasesort ( void )
public void natsort ( void )
public bool offsetExists ( mixed $index )
public mixed offsetGet ( mixed $index )
public void offsetSet ( mixed $index , mixed $newval )
public void offsetUnset ( mixed $index )
public string serialize ( void )
public void setFlags ( int $flags )
public void setIteratorClass ( string $iterator_class )
public void uasort ( callable $cmp_function )
public void uksort ( callable $cmp_function )
public void unserialize ( string $serialized )
}