Home  >  Article  >  Backend Development  >  How to store various numbers into an array in php

How to store various numbers into an array in php

PHPz
PHPzOriginal
2023-04-14 18:38:551684browse

In PHP, storing each number into an array is a very common operation. By using arrays we can easily manage large amounts of data and combine them together.

This article will introduce how to store each number into an array in PHP. We will cover the following topics:

  1. Creating an empty array
  2. Adding elements to an existing array
  3. Using a loop to store multiple numbers into an array
  4. Store numbers into an array using a specific key
  5. Create an empty array

First, we will create an empty array. In PHP, you can use the array() function to create an empty array.

$my_array = array();

Now, we have an empty array named $my_array. Next, we'll add elements.

  1. Add elements to an existing array

To add elements to the $my_array array, you can use the following code:

$my_array[] = 1;
$my_array[] = 2;
$my_array[] = 3;

In the above code , we added three elements to the array: 1, 2, and 3.

To view the elements in an array, use the var_dump() function:

var_dump($my_array);

Output:

array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

Each element of the array is enclosed in square brackets [], And they are sorted in the order in which they were added. Each element in the array has an index, the first element has index 0, the second element has index 1, and so on.

  1. Use a loop to store multiple numbers into an array

If we want to add multiple numbers to an array, adding them one by one will be tedious. In this case, we can use a loop to automatically add each number to the array.

Here is an example of adding multiple numbers to an array using a loop:

$my_array = array();

for ($i = 0; $i < 10; $i++) {
    $my_array[] = $i;
}

var_dump($my_array);

In the above code, we use a for loop to add the numbers 0 to 9 to the array.

Output:

array(10) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(2)
  [3]=>
  int(3)
  [4]=>
  int(4)
  [5]=>
  int(5)
  [6]=>
  int(6)
  [7]=>
  int(7)
  [8]=>
  int(8)
  [9]=>
  int(9)
}
  1. Use specific keys to store numbers into arrays

When adding elements to an array, you can use specific keys to store numbers into an array. Store in array. Using keys makes it easier for you to access elements in an array.

Here is an example of storing a number into an array using a specific key:

$my_array = array();

$my_array['apple'] = 1;
$my_array['banana'] = 2;
$my_array['orange'] = 3;

var_dump($my_array);

In the above code, we are using the name of each fruit as a key in the array.

Output:

array(3) {
  ["apple"]=>
  int(1)
  ["banana"]=>
  int(2)
  ["orange"]=>
  int(3)
}

Now we can access the elements in the array using the name of each fruit:

echo $my_array['apple'];

Output:

1

Summary

In PHP, it is not difficult to store each number in an array. You can create an empty array and then add elements to it. You can use a loop to automatically add multiple elements, or you can add elements to an array using a specific key. Using arrays makes it easier for you to manage large amounts of data.

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