Home > Backend Development > PHP Tutorial > Creation and traversal of arrays in php

Creation and traversal of arrays in php

WBOY
Release: 2016-07-29 09:15:46
Original
950 people have browsed it

The establishment of arrays in php can be divided into 3 types of methods. When traversing the array, we will often use the count($arr) function. This function returns the length of the array:

1.

//数组创建的第一种方法
	$arr[0] = 11;
	$arr[1] = "ee";
	$arr[2] = 11.1;
	$arr[3] = true;
	$arr[4] = null;
	for($i = 0; $i < count($arr); $i++) {
		echo &#39;<br/>'.$arr[$i];
	}
Copy after login

2.

//数组创建的第二种方法
	$arr = array(1,"ee",true);
	for($i = 0; $i < count($arr); $i++) {
		echo &#39;<br/>'.$arr[$i];
	}
Copy after login

3.

//数组创建的第三种方法(以键值对的方式进行存储)
	$arr['name'] = "raid";
	$arr['pwd'] = "eee";
	foreach($arr as $key=>$val) {
		echo $key."=".$val."<br/>";
	}
Copy after login

4. Sometimes it can also be written like this:

$arr = array(5=>"logo","ee"=>12,"ff"=>13);
	//显示整个数组的情况
	print_r($arr);
	echo "<br/>";
	var_dump($arr);
	echo "<br/>".is_array($arr);
Copy after login

In order to split the string into an array, we can use the following explode method:
$str = "e a d d";
	$arr = explode(" ", $str);
	foreach($arr as $key=>$val) {
		echo "<br/>".$key."=".$val;
	}
Copy after login

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces the creation and traversal of arrays in PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template