php array
1 Create array in php
$product = array('tires','oil','spark');//array() is actually a language structure rather than a function
2 If you need to create an ascending arrangement The numbers are stored in an array. You can use the range() function to automatically create the array
$numbers=range(1,10)//1 2 3 4 5 6 7 8 9 10
$numbers=range(1,10,2 )//1 3 5 7 9 The third parameter sets the step between parameters
range syntax:
range(first,second,step)//Create and return an array containing elements in the specified range
first: required. Specifies the minimum value of an array element.
second:required. Specifies the maximum value of an array element.
step: optional. Specifies the stepping between elements. The default is 1.
3 Use loop to access array
for loop to access array
for($i=0;i echo $product[i]." ";
}
foreach to access array
foreach($product as $pro){
echo $pro."";
}
4 Associative array
$prices= array('tires'=>100,'oil'=>200,'spark'=>30);
Access array$ prices['tires'] ,$prices['oil'],$prices['spark']
foreach access array
foreach ($price as $key =>$value){
echo $key.'-'.$ value.'
';
}
each() structure prints array
while($element = each($price)){
echo $element['key'].'-'.$element['value']. '
';
}
list() and each() print array
while(list($product,$pri)=each(prices)){
echo "$product - $pri
";
}
each( ) Syntax
each(array)// The function generates an array consisting of the key name and key value of the element pointed to by the current internal pointer of the array, and moves the internal pointer forward. The returned array contains four elements: key 0, 1, key and value. Cells 0 and key contain the key names of the array cells, and 1 and value contain the data.
If the internal pointer exceeds the range of the array, this function will return FALSE.
array: required. Specify the array to be used
list() syntax:
list(var1,var2...)//Assign a value to a set of variables using elements in the array. List is actually a structure, not a function, similar to array
var1 is required. The first variable to be assigned a value.
var2 optional. There can be multiple variables.
5 multi-dimensional array
$products=array(
array('Tire','tires','100'),
array('Oil','oil','300'),
array('Spak',' Speak','100'),
)
Loop multi-dimensional array
foreach($products as $value){
foreach($value as $va){
echo $va.'';
}
}
6 array Sort
sort(array,sorttype)//Sort the values of the given array in ascending order
array: required. The input array.
sorttype: optional. Specifies how to arrange the values of an array. Possible values:
•SORT_REGULAR - Default. Processed with their original type (without changing type).
•SORT_NUMERIC - Handle values as numbers
•SORT_STRING - Handle values as strings
•SORT_LOCALE_STRING - Handle values as strings, based on local settings*.
Example:
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
sort($my_array);
print_r ($my_array);
?>
Output:
Array
(
[0] => Cat
[1] => Dog
[2] => Horse
)
asort(array,sorttype) //Perform on the array Sort and maintain index relationships. Mainly used to sort associative arrays where the order of units is important
Array: Required. The input array.
sorttype: optional. Specifies how to arrange the values of an array. Possible values:
•SORT_REGULAR - Default. Processed with their original type (without changing type).
•SORT_NUMERIC - Treat values as numbers
•SORT_STRING - Handle values as strings
•SORT_LOCALE_STRING - Handle values as strings, based on local settings*.
Example:
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
asort($my_array);
print_r($my_array);
?>
Output:
Array
(
[b] => Cat
[a] => Dog
[c] => Horse
)
ksort(array,sorttype)//according to key Sort the array by name, retaining the original keys for the array values
Array: Required. Specifies the array to be sorted.
sorttype: optional. Specifies how to arrange the values of an array. Possible values:
•SORT_REGULAR - Default. Processed with their original type (without changing type).
•SORT_NUMERIC - Handle the value as a number
•SORT_STRING - Handle the value as a string
•SORT_LOCALE_STRING - Handle the value as a string, based on local settings*.
Example:
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
ksort($my_array);
print_r ($my_array);
?>
Output:
Array
(
[a] => Dog
[b] => Cat
[c] => Horse
)
Reverse sort : rsort(),arsort() ,krsort()//For reverse sorting (descending order)
usort(array, sorttype)//Use user-defined function to sort the array.
array: required. Specifies the array to be sorted.
sorttype: required. User-defined functions.
The function must be designed to return -1, 0, or 1, and should accept two arguments for comparison, and work in a manner similar to the following:
•If a = b, return 0
•If a > b, Return 1
•If a Example:
function my_sort($a, $b)
{
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
}
$arr = array("Peter", "glenn","Cleveland","peter","cleveland", "Glenn");
usort($arr, "my_sort");
print_r ($arr);
?>
Output result:
Array
(
[0] => peter
[1] => glenn
[2] => cleveland
[3] = > Peter
[4] => Glenn
[5] => Cleveland
)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
