search
Homephp教程php手册《PHP编程最快明白》第三讲:php数组

数组在上一讲开始提及了,并初始化了一个关联数组。PHP数组不声明也可以也能用的,比如str[]=”值”;即可。

实例7:数组值基本操作

复制代码 代码如下:


$arr=array('a'=>"你",'b'=>"我","他");
$arr[]="其他";
echo $arr['b']."
";
$arr['c']="";//给个空值,但仍然占着位置的
echo count($arr)."
";//数组有多少个值。
unset($arr['b']);//这函数可以注销字符串、整个数组等值类型和引用类型。
print_r($arr);//这函数打印数值、引用类型的整个内部结构。
echo "
";
foreach($arr as $key=>$value)
echo $key.":".$value."
";//循环输出整个数组的值。
?>


实例8:数组和字符串之间转换

复制代码 代码如下:


$arr=array('a'=>"你",'b'=>"我","他");
echo $arr=implode('-',$arr);//数组转字符串,连接符-
echo "
";
print_r(explode('-',$arr,2));//字符串转数组。如果最后一个参数不用,表示全部'-'都分割成数组
?>


实例9:数组排序

复制代码 代码如下:


$arr=array('b'=>"你",'a'=>"我","他");
ksort($arr);//数组按键值的拼音(UTF-8编码)排序,键值不会丢失。注意此排序不返回新数组而是直接把原来数组当引用传递。
print_r($arr);
echo "
";
asort($arr);//数组按值的拼音(UTF-8编码)排序,键值不会丢失。如果不要键值,可以用函数sort();如果反序也有函数rsort()。注意此排序不返回新数组而是直接把原来数组当引用传递。
print_r($arr);
echo "
";
$arr=array(10000,100,1000);
natsort($arr);//按数字自然排序值,而natcasesort()不区分大小写
print_r($arr);
echo "
";
print_r(array_reverse($arr));//数组反序
echo "
";
?>


实例10:数组、数字随机抽取、数字和编码转换

复制代码 代码如下:


$arr=array('b'=>"你",'a'=>"我","他");
$key=array_rand($arr,2);//数组随机抽取2个键值,返回含两个键值的索引数组
echo $arr[$key[0]].$arr[$key[1]];
echo "
";
echo mt_rand(60,100);//返回一个该范围内的随机整数。
echo "
";
echo chr(mt_rand(ord('a'),ord('z')));//数字和编码转换。
echo "
";
?>


数组的函数就讲完了,香港服务器租用,网站空间,我只挑几个有代表性的让大家入门,其实,还有一些不常用的数组函数的。除此之外,我们可以用for或者foreach循环来处理数组,生成自己的my_函数,笨有笨方法。 ,美国服务器
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.