search
HomeBackend DevelopmentPHP ProblemWhat are the two types of arrays in php?
What are the two types of arrays in php?Sep 20, 2022 pm 07:51 PM
phpphp array

Two types of php arrays: 1. Index array. The subscript (key name) consists of numbers. It automatically increases from 0 by default. Each number corresponds to the position of an array element in the array. 2. Associative array, the subscript (key name) consists of a string or a mixture of strings and numbers; if a key name in an array is not a number, then the array is an associative array.

What are the two types of arrays in php?

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

In the PHP array, there will be a key name The value corresponds to it, which is a key/value pair.

What are the two types of arrays in php?

According to the different data types of array key names, we can divide PHP arrays into two types:

  • An array with numbers as keys is called an Indexed Array;

  • An array with strings or a mixture of strings and numbers as keys is called an Associative Array. ).

1. Index array

The subscript (key name) of the index array consists of numbers, starting from 0 by default, and each number corresponds to The position of an array element in the array does not need to be specified. PHP will automatically assign an integer value to the key name of the index array, and then automatically increase from this value.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$array=array(1,2,3,4,5,6,7,8,9,10);
var_dump($array);//打印数组
?>

What are the two types of arrays in php?

2. Associative array

The subscript (key name) of the associative array is composed of a mixture of numerical values ​​and strings , if a key name in an array is not a number, then the array is an associative array.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$array=array("id"=>1,"name"=>"李华","age"=>23,"1"=>1,"id2"=>52);
var_dump($array);//打印数组
?>

What are the two types of arrays in php?

The key name of an associative array can be any integer or string. If the key name is a string, add a delimiting modifier to the key name - single quote '' or double quote "". For indexed arrays, in order to avoid confusion, it is best to add delimiters.

Extended knowledge: mutual conversion between index array and associative array

Associative array to index array

In php, you can use the array_values() function to convert an associative array into an index array.

array_values($array) The function is to return the values ​​of all elements in the array. It is very simple to use. With only one required parameter, you can return a value containing all the elements in the given array. Array of values, but no key names. The returned array will be in the form of an indexed array, with array indices starting at 0 and increasing by 1.

Simply put, you can use this function to reset the array key name and convert the key name with confusing string or numerical value into a numeric key name starting from 0 and increasing by 1.

array_values() function is particularly suitable for arrays with confusing element subscripts, or for converting associative arrays into indexed arrays.

<?php
$arr=array("Peter"=>65,"Harry"=>80,"John"=>78,"Clark"=>90);
var_dump($arr);
var_dump(array_values($arr));
?>

What are the two types of arrays in php?

Convert index array to associative array

In php, you can use the array_combine() function to convert the index array into an associative array array.

array_combine($keys,$values)The function creates a new array by merging two arrays, where the elements in the $keys array serve as the keys of the new array, $ The elements of the values ​​array serve as the key values ​​of the new array.

But it should be noted that when using the array_combine() function to create an array, the number of elements in the $keys array and the $values ​​array must be consistent, so that the key names and key values ​​can correspond one to one, otherwise An error will be reported and FALSE will be returned.

And the $keys array cannot be a multi-dimensional array, otherwise an error will be reported; but the $values ​​array can be a multi-dimensional array.

<?php
header("Content-type:text/html;charset=utf-8");
$keys=array("a","b","c","d");
$values=array("red","green","blue","yellow");
var_dump($keys);
var_dump($values);
echo "使用array_combine()合并数组后:";
var_dump(array_combine($keys,$values));
?>

What are the two types of arrays in php?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What are the two types of arrays in php?. For more information, please follow other related articles on the PHP Chinese website!

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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

See all articles

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

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

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.

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

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),