Home  >  Article  >  Backend Development  >  How to use arrays in PHP (with code example)

How to use arrays in PHP (with code example)

WBOY
WBOYforward
2024-03-01 18:52:05644browse

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>

Arrays can be initialized with values:

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

Arrays can hold any type of value:

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

Even other arrays:

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

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>

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

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

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

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

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

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

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>

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>

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>
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!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete