PHP数组操作及遍历实例大全

标签: php数组遍历   时间:2017-07-27
本文章向大家介绍PHP数组函数和遍历数组的实例,需要的朋友可以参考一下。

对于Web编程来说,最重要的就是存取和读写数据了。存储方式可能有很多种,可以是字符串、数组、文件的形式等。数组,可以说是PHP的数据应用中较重要的一种方式。PHP的数组函数众多,下面是我学习的小结,借此记之,便于以后鉴之。

1. 数组定义

数组的定义使用 array()方式定义,可以定义空数组:

<?php
    $number = array(1,3,5,7,9);
    //定义空数组
    $result = array();
    $color =array("red","blue","green");
    //自定义键值
    $language = (1=>"English",3=>"Chinese",5=>"Franch");
    //定义二维数组
    $two = array(
                "color"=>array("red","blue"),    //用逗号结尾
                "week"=>array("Monday","Friday")    //最后一句没有标点
    );
?>

 

2. 创建数组

compact()

compact()函数——将一个或多个变量(包含数组)转换为数组:array compact ( mixed $varname [, mixed $... ] )。

<?PHP
    $number = "1,3,5,7,9";
    $string = "I'm PHPer";
    $array = array("And","You?");
    $newArray = compact("number","string","array");
    print_r ($newArray);
?>

compact()函数用于将两个或多个变量转换为数组,当然也包含数组变量。其参数是变量的名称而非带有$全名。相反的函数是extract()作用顾名思义就是将数组转换为单个的字符串,键值作为其字符串名称,数组值作为字符串的值。

运行结果:

Array ( 
	[number] => 1,3,5,7,9 
	[string] => I'm PHPer 
	[array] => Array ( [0] => And [1] => You? ) 
)

 

array_combine()

array_combine()——将两个数组重组成一个数组,一个作键值一个做的值:array array_combine ( array $keys , array $values )

<?PHP
    $number = array("1","3","5","7","9");
    $array = array("I","Am","A","PHP","er");
    $newArray = array_combine($number,$array);
    print_r ($newArray);
?>

array_combine函数不多说了,谁看了都明白。

运行结果:

Array ( [1] => I [3] => Am [5] => A [7] => PHP [9] => er )

range()

range()函数——创建指定范围的数组:

<?PHP
    $array1 = range(0,100,10);//0为起始值,100为结束值,10为步进值(默认步进值为1).
    print_r($array1);
    echo"<br />";
    $array2 = range("A","Z");
    print_r($array2);
    echo "<br />";
    $array3 = range("z","a");
    print_r($array3);
?>

 

array_fill()

array_fill()函数——填充数组函数:

<?PHP
        $array = range(1,10);
        $fillarray = range("a","d");
        $arrayFilled = array_fill(0,5,$fillarray);//这里的$fillarray可以是字符串,如"test".
        echo "<pre>";
        print_r ($arrayFilled);
        echo "</pre>";
        $keys = array("string","2",9,"SDK","PK");
        $array2 = array_fill_keys($keys,"testing");
        echo "<pre>";
        print_r ($array2);
        echo "</pre>";
?>

运行结果:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
        )

    [1] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
        )

    [2] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
        )

    [3] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
        )

    [4] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
        )

)
Array
(
    [string] => testing
    [2] => testing
    [9] => testing
    [SDK] => testing
    [PK] => testing
)

 

3. 数组的遍历

foreach遍历

foreach (array_expression as $value){}

foreach (array_expression as $key => $value){}

<?PHP
	$speed = array(50,120,180,240,380);
	foreach($speed as $keys=>$values){
		echo $keys."=>".$values."<br />";
	}
?>

 

 

while循环遍历

while循环遍历一般结合list函数,以下是实例

	<?PHP
        $staff = array(
            array("姓名","性别","年龄"),
            array("小张","男",24),
            array("小王","女",25),
            array("小李","男",23)
        );
        echo "<table border=2>";
        while(list($keys,$value) = each($staff)){
            list($name,$sex,$age) = $value;
            echo "<tr><td>$name</td><td>$sex</td><td>$age</td></tr>";
        }
        echo "</table>";
       ?>

 

for循环遍历

<?PHP
    $speed = range(0,220,20);
    for($i =0;$i<count($speed);$i++) {
        echo $speed[$i]." ";
    }
?>

运行结果:

0 20 40 60 80 100 120 140 160 180 200 220 

 

总结

数组的使用在PHP中至关重要,由于PHP没有指针,所以数组承担了很大的数据操作任务。学好数组,才能把PHP应用的得心应手,这里所列均是常用的PHP数组相关的函数及用法,欢迎一起学习!

原文地址:http://www.manongjc.com/article/1869.html