How to store data into an array in php

WBOY
Release: 2023-05-06 14:35:07
Original
724 people have browsed it

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");
Copy after login

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"
);
Copy after login

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";
Copy after login

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");
Copy after login

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]
Copy after login

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" => ""
);
Copy after login

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";
Copy after login

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));  // 合并数组成为关联数组
Copy after login

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!

source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!