How to use arrays in PHP (with code example)

WBOY
Release: 2024-03-01 18:54:01
forward
649 people have browsed it

The array in PHP is a very commonly used data structure that can be used to store multiple values. In PHP, using arrays makes it easier to manage and manipulate large amounts of data. This article will introduce how to use arrays in PHP and provide code examples to help readers better understand the basic usage and operation techniques of arrays. Let's explore together how to flexibly apply array functions in PHP and improve programming efficiency!

You can define an empty array in two different ways:

<code>$list = [];$list = array();</code>
Copy after login

Arrays can be initialized with values:

<code>$list = [1, 2];$list = array(1, 2);</code>
Copy after login

Arrays can hold any type of value:

<code>$list = [1, 'test'];</code>
Copy after login

Even other arrays:

<code>$list = [1, [2, 'test']];</code>
Copy after login

You can access elements in an array using this notation:

<code>$list = ['a', 'b'];$list[0]; //'a' --the index starts at 0$list[1]; //'b'</code>
Copy after login

Once an array is created, you can append values ​​to it this way :

<code>$list = ['a', 'b'];$list[] = 'c';</code>
Copy after login

You can use array_unshift() instead of adding items at the beginning of the array:

<code>$list = ['b', 'c'];array_unshift($list, 'a');</code>
Copy after login

Use the built-in count() function to calculate How many items are in an array:

<code>$list = ['a', 'b'];count($list); //2</code>
Copy after login

Use the in_array() built-in function to check if an array contains an item:

<code>$list = ['a', 'b'];in_array('b', $list); //true</code>
Copy after login

If in addition to confirming the presence, you Index is also needed, use array_search():

<code>$list = ['a', 'b'];array_search('b', $list) //1</code>
Copy after login

Useful PHParray functions

with characters StringsLike numbers, php provides many very useful functions for arrays. We've already seen count(),in_array(),array_search(), let's look at some more:

  • is_array() Check if a variable is an array

  • array_unique() Remove duplicate values ​​from an array

  • array_search() Search for a value in the array and return the key value

  • array_reverse() Reverse Convert an array

  • array_reduce() Use a callback function to reduce an array to a single value

  • array_map()Apply a callback function to each item in the array. Usually used to create a new array by modifying the value of an existing array without changing the array

  • array_filter() Use a callback function to Filter the array to a single value

  • max() Get the maximum value in the array

  • min () Get the minimum value contained in the array

  • array_rand() Get a random item from the array

  • array_count_values() Count all the values ​​in the array

  • implode() Turn an array into a string

  • array_pop() Removes the last item from the array and returns its value

  • array_shift() Same as but removes the first item instead of the last itemarray_pop()

  • sort() on an array Sort

  • ##rsort() Sort an array in reverse order

  • array_walk () Similar to, does something with each item in the array, but in addition, it can also change the values ​​in the existing array. array_map()

Associative array

So far, we have used arrays with increasing, numeric indexes. 0, 1, 2...

You can also use arrays with named indexes (keys), we call them associative arrays:

<code>$list = ['first' => 'a', 'second' => 'b'];$list['first'] //'a'$list['second'] //'b'</code>
Copy after login
We have some that are particularly useful for associative arrays Function:

  • array_key_exists() Check if a key exists in the array

  • array_keys() Get all the keys from the array

  • ##array_values()

    Get all the values ​​from the array

  • asort()

    Sort an associative array by value

  • arsort()

    Sort an associative array by value in descending order

  • ksort()

    Sort the associative array by key

  • krsort()

    Sort the associative array in descending order by key Sort

The above is the detailed content of How to use arrays in PHP (with code example). For more information, please follow other related articles on the PHP Chinese website!

source:lsjlt.com
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