Rumah > Artikel > pembangunan bahagian belakang > php常用数组函数查询手册 php中的数组 php打印数组 php声明数
php数组常用函数
返回一个数组,该数组包含了 arr1 中的所有单元经过 callback 作用过之后的单元。 callback 接受的参数数目应该和传递给 array_map() 函数的数组数目一致。
<code><span>/*例1*/</span><span><?php </span><span><span>function</span><span>cube</span><span>( <span>$n</span> )</span> {</span><span>return</span>( <span>$n</span> * <span>$n</span> * <span>$n</span> ); } <span>$a</span> = <span>array</span>( <span>1</span> , <span>2</span> , <span>3</span> , <span>4</span> , <span>5</span> ); <span>$b</span> = array_map ( <span>"cube"</span> , <span>$a</span> ); print_r ( <span>$b</span> ); <span>/** 输出 Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 ) **/</span><span>?></span></span></code>
<code><span><?php </span><span>/*例2*/</span><span>/*将多个数组进行合并*/</span><span>$a</span> = <span>array</span>( <span>1</span> , <span>2</span> , <span>3</span> , <span>4</span> , <span>5</span> ); <span>$b</span> = <span>array</span>( <span>"one"</span> , <span>"two"</span> , <span>"three"</span> , <span>"four"</span> , <span>"five"</span> ); <span>$c</span> = <span>array</span>( <span>"uno"</span> , <span>"dos"</span> , <span>"tres"</span> , <span>"cuatro"</span> , <span>"cinco"</span> ); <span>$d</span> = array_map ( <span>null</span> , <span>$a</span> , <span>$b</span> , <span>$c</span> ); print_r ( <span>$d</span> ); <span>/*输出 Array ( [0] => Array ( [0] => 1 [1] => one [2] => uno ) [1] => Array ( [0] => 2 [1] => two [2] => dos ) [2] => Array ( [0] => 3 [1] => three [2] => tres ) [3] => Array ( [0] => 4 [1] => four [2] => cuatro ) [4] => Array ( [0] => 5 [1] => five [2] => cinco ) ) */</span><span>?></span></span></code>
step表示间隔值,不写默认为1
<code><span>/* 例1,产生一组数字 */</span><span>$nums</span> = range(<span>1</span>, <span>5</span>); print_r(<span>$nums</span>); <span>/* 输出: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) */</span><span>$nums</span> = range(<span>1</span>, <span>5</span>, <span>2</span>); print_r(<span>$nums</span>); <span>/* 输出: Array ( [0] => 1 [1] => 3 [2] => 5 ) */</span></code>
<code><span>/* 例2,产生一组字母数组 */</span><span>$array</span> = range(<span>'a'</span>,<span>'f'</span>); print_r(<span>$array</span>); <span>/* 输出: Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f ) */</span><span>$array</span> = <span>array</span>(<span>'a'</span>, <span>'f'</span>, <span>2</span>); print_r(<span>$array</span>); <span>/* 输出: print_r($array); /* 输出: Array ( [0] => a [1] => c [2] => e ) */</span></code>
array_merge() 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。
如果输入的数组中有相同的字符串键名,则该键名后面的值将覆盖前一个值。然而,如果数组包含数字键名,后面的值将不会覆盖原来的值,而是附加到后面。
如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引。
<code><span>/* 解释:如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引。 */</span><span>$array1</span> = <span>array</span>(<span>1</span>, <span>2</span>, <span>3</span>, <span>4</span>, <span>5</span>); <span>$array2</span> = <span>array</span>(<span>1</span>, <span>2</span>, <span>8</span>, <span>9</span>); <span>$array3</span> = array_merge(<span>$array1</span>, <span>$array2</span>); print_r(<span>$array3</span>); <span>/* 输出: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 1 [6] => 2 [7] => 8 [8] => 9 ) */</span></code>
如果输入的数组中有相同的字符串键名,则这些值会被合并到一个数组中去,这将递归下去,因此如果一个值本身是一个数组,本函数将按照相应的条目把它合并为另一个数组。然而,如果数组具有相同的数组键名,后一个值将不会覆盖原来的值,而是附加到后面。
会根据键名相同一层一层的将值进行合并
<code><span>/* 例1 */</span><span>$ar1</span> = <span>array</span>(<span>"color"</span> => <span>array</span>(<span>"favorite"</span> => <span>"red"</span>), <span>5</span>); <span>$ar2</span> = <span>array</span>(<span>10</span>, <span>"color"</span> => <span>array</span>(<span>"favorite"</span> => <span>array</span>(<span>'a'</span>=><span>"red"</span>), <span>"blue"</span>)); <span>$result</span> = array_merge_recursive(<span>$ar1</span>, <span>$ar2</span>); print_r(<span>$result</span>); <span>/* 输出: Array ( [color] => Array ( [favorite] => Array ( [0] => red [a] => red 重点 ) [0] => blue ) [0] => 5 [1] => 10 ) */</span></code>
<code><span>/* 例2 */</span><span>$ar1</span> = <span>array</span>(<span>"color"</span> => <span>array</span>(<span>"favorite"</span> => <span>"red"</span>), <span>5</span>); <span>$ar2</span> = <span>array</span>(<span>10</span>, <span>"color"</span> => <span>array</span>(<span>"favorite"</span> =><span>"red"</span>, <span>"blue"</span>)); <span>$result</span> = array_merge_recursive(<span>$ar1</span>, <span>$ar2</span>); print_r(<span>$result</span>); <span>/* 输出: Array ( [color] => Array ( [favorite] => Array ( [0] => red [1] => red 重点 ) [0] => blue ) [0] => 5 [1] => 10 ) */</span></code>
<code><span><?php </span><span>$input</span> = <span>array</span>(<span>12</span>, <span>10</span>, <span>9</span>); <span>$result</span> = array_pad(<span>$input</span>, <span>5</span>, <span>0</span>); <span>// result is array(12, 10, 9, 0, 0)</span><span>$result</span> = array_pad(<span>$input</span>, -<span>7</span>, -<span>1</span>); <span>// result is array(-1, -1, -1, -1, 12, 10, 9)</span><span>$result</span> = array_pad(<span>$input</span>, <span>2</span>, <span>"noop"</span>); <span>// not padded</span><span>/* 如果size<span>?></span></span></span></code>
array_pop() 弹出并返回 array 数组的最后一个单元,并将数组 array 的长度减一。 如果 array 为空(或者不是数组)将返回 NULL 。 此外如果被调用不是一个数则会产生一个 Warning。
<code><span><?php </span><span>$stack</span> = <span>array</span>(<span>"orange"</span>, <span>"banana"</span>, <span>"apple"</span>, <span>"raspberry"</span>); <span>$fruit</span> = array_pop(<span>$stack</span>); print_r(<span>$stack</span>); <span>?></span><span>/* 输出 Array ( [0] => orange [1] => banana [2] => apple ) */</span></span></code>
array_shift() 将 array 的第一个单元移出并作为结果返回,将 array 的长度减一并将所有其它单元向前移动一位。所有的数字键名将改为从零开始计数,文字键名将不变。 如果 array 为空(或者不是数组)将返回 NULL
array_push()
reset()
end()
test() 格式化输出数组
array_rand()
array_replace()
array_reverse()
array_sum()
sort() 将值排序完成后,会是一个索引数组,即便原来是一个关联数组
usort()
rsort()
ksort()
krsort()
uksort()
asort()
arstor()
in_array()
array_splice()
以上就介绍了php常用数组函数查询手册,包括了数组函数,php方面的内容,希望对PHP教程有兴趣的朋友有所帮助。