Home  >  Article  >  Backend Development  >  How to store data as array in php

How to store data as array in php

PHPz
PHPzOriginal
2023-04-27 15:54:32510browse

PHP is a widely used open source scripting language that allows data to be easily manipulated and stored in various data types. Among them, storing as an array is a common way because arrays can hold multiple data and can easily process and access these data.

In PHP, an array is an ordered collection where each element can be accessed by its index or key. Here's how to store data as an array in PHP:

  1. Create an empty array

You can create an empty array using the following syntax:

$myArray = array();
  1. Create an array with elements

You can create an array with multiple elements using the following syntax:

$myArray = array('元素1', '元素2', '元素3');
  1. Add manually using subscripts and values Elements

Elements can be added to an array manually using the following syntax:

$myArray = array();
$myArray[0] = '元素1';
$myArray[1] = '元素2';
$myArray[2] = '元素3';

Alternatively, the keys and values ​​can be specified when declaring the array:

$myArray = array(
    '键1' => '值1',
    '键2' => '值2',
    '键3' => '值3'
);
  1. Use loops to add elements

You can use loop statements to add multiple elements, such as for loops or foreach loops:

$myArray = array();
for ($i = 0; $i < 10; $i++) {
    $myArray[] = $i * 2;
}

foreach ($myArray as $value) {
    echo $value . &#39; &#39;;
}

The above code adds 10 elements using a for loop elements into the array, and use a foreach loop to loop through each element in the array.

  1. Read data from the database and store it as an array

You can use PHP's built-in database extension functions, such as mysqli or PDO, to read data from the database and store it as an array. It is stored as an array.

// 连接到数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$conn = new mysqli($servername, $username, $password, $dbname);

// 查询数据并存储为数组
$sql = "SELECT * FROM myTable";
$result = $conn->query($sql);
$myArray = array();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $myArray[] = $row['columnName'];
    }
}

// 输出数组
print_r($myArray);

// 关闭数据库连接
$conn->close();

The above code uses the mysqli extension function to query all data from the database and store it in an array. Finally, use the print_r function to output the array. It should be noted that in actual applications, the database connection should be closed to release resources.

To sum up, storing as an array is a common data storage method in PHP. By understanding the different storage methods and methods, you can better process and manipulate data using PHP.

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