本文实例讲述了PHP实现C#山寨ArrayList的方法。分享给大家供大家参考。具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
class ArrayList
{
public $length ;
public $name ;
public $my_array ;
function __construct()
{
$this ->my_array=Array();
}
public function Add( $element )
{
array_push ( $this ->my_array, $element );
}
public function get_Length()
{
$this ->length= count ( $this ->my_array);
return $this ->length;
}
public function get_Element( $key )
{
if ( array_key_exists ( $key , $this ->my_array))
{
echo $this ->my_array[ $key ];
}
else
{
echo "没有这个元素" ;
}
}
public function list_array()
{
foreach ( $this ->my_array as $value )
{
echo $value ;
echo "<br/>" ;
}
}
public function Delete ( $key )
{
if ( array_key_exists ( $key , $this ->my_array))
{
$this ->my_array[ $key ]=null;
}
else
{
echo "没有这个元素" ;
}
}
public function erase_number()
{
$pattern = "/[0-9]/" ;
foreach ( $this ->my_array as $value )
{
if ( eregi ( $pattern , $value ))
{
$value =null;
}
}
foreach ( $this ->my_array as $value )
{
echo $value ;
echo "<br/>" ;
}
}
public function erase_char()
{
$pattern = '/a-zA-Z/' ;
for ( $i =0; $i < count ( $this ->my_array)-1; $i ++)
{
if ( eregi ( $pattern , $this ->my_array[ $i ]))
{
$this ->my_array[ $i ]=null;
}
}
foreach ( $this ->my_array as $value )
{
echo $value ;
echo "<br/>" ;
}
}
}
|
希望本文所述对大家的php程序设计有所帮助。