Home >Backend Development >PHP Problem >How to store data into an array in php

How to store data into an array in php

WBOY
WBOYOriginal
2023-05-06 14:35:07772browse

PHP is an extremely powerful programming language that is widely used in web development. When processing data, PHP has many built-in functions and methods for processing data. Among them, array is the most commonly used data type in PHP and one of the most effective ways to process PHP data. Therefore, this article will explore how PHP stores data into an array.

  1. Array basics

In PHP, an array is a variable that can store multiple values. Each array element has a unique key value, which can be a number or a string. There are two types of arrays in PHP: indexed arrays and associative arrays.

The index array refers to an array whose key value is a number, starting from 0. For example:

$fruits = array("apple", "banana", "orange");

In this array, $fruits[0] represents "apple", $fruits[1] represents "banana", and $fruits[2] represents "orange".

Associative array refers to an array whose key value is a string. For example:

$person = array(
    "name" => "Tom",
    "age" => 18,
    "gender" => "male"
);

In this array, $person["name"] represents "Tom", $person["age"] represents 18, and $person["gender"] represents "male".

  1. How to store data into an index array

In PHP, we can use the array() function to create an empty array, and then use the [] operator to Elements are added to the array. For example:

$fruits = array();   // 创建一个空的数组

$fruits[] = "apple"; // 添加元素
$fruits[] = "banana";
$fruits[] = "orange";

In this way, the $fruits array is created and contains three elements "apple", "banana" and "orange".

In addition to using the [] operator to add elements, we can also use the array_push() function to add elements to the end of the array. For example:

$fruits = array();   // 创建一个空的数组

array_push($fruits, "apple");
array_push($fruits, "banana");
array_push($fruits, "orange");

In this way, the $fruits array is created and contains three elements "apple", "banana" and "orange".

In addition to adding elements, we can also use the [] operator to modify the element values ​​in the array. For example:

$fruits = array("apple", "banana", "orange");

$fruits[1] = "pear";   // 修改$fruits[1]

In this way, the second element in the $fruits array (that is, "banana") will be modified to "pear".

  1. How to store data into an associative array

In PHP, you can use the array() function to create an associative array, and you can use keys to reference array elements. For example:

$person = array(
    "name" => "",
    "age" => 0,
    "gender" => ""
);

In this way, we create an associative array named $person and contains three elements.

Next, we can use the key name (that is, the string name) to access or modify the value of the array element. For example:

$person["name"] = "Tom";
$person["age"] = 18;
$person["gender"] = "male";

In this way, the three elements in the $person array are assigned values.

In addition to using the above methods to add and modify the values ​​of array elements, we can also use two functions: array_merge() and array_combine(). The array_merge() function is used to merge two arrays, while the array_combine() function is used to merge two arrays into an associative array.

For example:

$person1 = array("name" => "Tom");
$person2 = array("age" => 18);
$person = array_merge($person1, $person2);  // 合并数组

$person3 = array("gender" => "male");
$person = array_combine(array_keys($person), array_merge($person, $person3));  // 合并数组成为关联数组

In this way, the $person array contains three elements and is an associative array.

Summary

This article briefly introduces arrays in PHP, their types and basic operations, and also explains how to store data into arrays and how to create and add elements to arrays. For PHP developers, this knowledge is very important, especially when it comes to handling data and building applications. By studying this article, I hope to better master PHP arrays and improve my development level.

The above is the detailed content of How to store data into an array 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