﻿ 数组操作、sort、asort、ksort、array_push、array_shif-PHP编程_网络编程-脚本宝典

# 数组操作、sort、asort、ksort、array_push、array_shif

## 数组操作、sort、asort、ksort、array_push、array_shif

php数组变量赋值当使用array的时候能简单地赋值数组变量。\$a = array(‘abc’, ‘def’, ‘ghi’);分别代入时，生成如下数据。\$a[0] = ‘abc’;\$a[1] = ‘def’;\$a[2] = ‘ghi’;如果省略了下

#### 分解的字符串

\$a[0] = ’0′、\$a[1] 可见= ’1′

```\$a = '0123';
print "```

```\$a[0]

\n"; print "
\$a[1]

\n"; print "
\$a[2]

\n"; print "
\$a[3]

\n";```

#### 获取数组数

```	\$a = array('abc', 'def', 'ghi');
\$ct = count(\$a);```

#### 排列列举参照

\$a = array(‘abc’, ‘def’, ‘ghi’); 在3是使用代码reset、while、each重置。
\$tmp [0]是下标字符，\$tmp [1] 是实际数据。
```    reset(\$a);
while (\$tmp = each(\$a)) {
print "```

\$tmp[0] - \$tmp[1]

\n"; }

#### 数组排序（升序）

```    \$a = array('abc', 'def', 'ghi')
sort(\$a);

foreach (\$a as \$tmp) {
print "```

```\$tmp

\n"; }```

#### 数组排序（降序）

```    \$a = array('abc', 'def', 'ghi');
rsort(\$a);

foreach (\$a as \$tmp) {
print "```

```\$tmp

\n"; }```

#### 关联数组排序（升序）

```	\$a = array('key1' => 'abc', 'key2' => 'def', 'key3' => 'ghi');
asort(\$a);

foreach (\$a as \$key => \$tmp) {
print "```

```\$key - \$tmp

\n"; }```

#### 关联数组排序（降序）

```    \$a = array('key1' => 'abc', 'key2' => 'def', 'key3' => 'ghi');
arsort(\$a);

foreach (\$a as \$key => \$tmp) {
print "```

```\$key - \$tmp

\n"; }```

#### 关联数组键名排序（升序）

```    \$a = array('key1' => 'abc', 'key2' => 'def', 'key3' => 'ghi');
ksort(\$a);

foreach (\$a as \$key => \$tmp) {
print "```

```\$key - \$tmp

\n"; }```

#### 关联数组键名排序（降序）

```	\$a = array('key1' => 'abc', 'key2' => 'def', 'key3' => 'ghi');
krsort(\$a);

foreach (\$a as \$key => \$tmp) {
print "```

```\$key - \$tmp

\n"; }```

#### 数组尾部添加

```    \$a = array('abc', 'def', 'ghi');

array_push(\$a, 'data1', 'data2');

foreach (\$a as \$tmp) {
print "```

```\$tmp

\n"; }```

#### 数组尾部删除

```    \$a = array('abc', 'def', 'ghi');

array_pop(\$a, 'data1', 'data2');

foreach (\$a as \$tmp) {
print "```

```\$tmp

\n"; }```

#### 数组首部添加

```    \$a = array('abc', 'def', 'ghi');

array_unshift(\$a, 'data1', 'data2');

foreach (\$a as \$tmp) {
print "```

```\$tmp

\n"; }```

#### 数组首部删除

```    \$a = array('abc', 'def', 'ghi');

array_shift(\$a);

foreach (\$a as \$tmp) {
print "```

```\$tmp

\n"; }```

#### 数组连结

```    \$a = array('abc', 'def', 'ghi');
\$b = array('123', '456', '789');

\$a = array_merge(\$a, \$b);

foreach (\$a as \$tmp) {
print "```

```\$tmp

\n"; } ```

Tags：

﻿
<