Home  >  Article  >  Backend Development  >  How to get columns from php two-dimensional array

How to get columns from php two-dimensional array

PHPz
PHPzOriginal
2023-04-18 09:05:48987browse

In PHP language, array is a commonly used data structure. A two-dimensional array means that each element in the array is also an array. When processing a two-dimensional array, sometimes you need to remove a certain column. This article will introduce how to remove a column of a two-dimensional array in PHP.

1. Use loop traversal

To take out the columns of a two-dimensional array, the simplest way is to use a loop to traverse the array. You can first loop through a column of all elements in the array, and then combine these columns into a new array. The following code is an example:

$students = array( 
array('name'=>'Alice', 'age'=>18, 'score'=>89), 
array('name'=>'Bob', 'age'=>19, 'score'=>92), 
array('name'=>'Cathy', 'age'=>18, 'score'=>91), 
array('name'=>'David', 'age'=>20, 'score'=>87), 
); 

$scores = array(); 
foreach ($students as $student) { 
    $scores[] = $student['score']; 
} 

print_r($scores);

The above code will take out the score column of all elements in the two-dimensional array $students and form it into a new array $scores. Here a foreach loop is used to traverse the array, the corresponding score value is taken out for each array element, and added to the new array $scores.

2. Use the array_column function

PHP provides the array_column function, which can easily extract a column of a two-dimensional array. The sample code is as follows:

$students = array( 
array('name'=>'Alice', 'age'=>18, 'score'=>89), 
array('name'=>'Bob', 'age'=>19, 'score'=>92), 
array('name'=>'Cathy', 'age'=>18, 'score'=>91), 
array('name'=>'David', 'age'=>20, 'score'=>87), 
); 

$scores = array_column($students, 'score'); 

print_r($scores);

The above code uses the array_column function to take out the score column of all elements in the two-dimensional array $students and form it into a new array $scores. Among them, the first parameter is a two-dimensional array, and the second parameter is the column name to be extracted.

It should be noted that the array_column function requires PHP version 5.5.0 and above, otherwise an error will be reported.

3. Use the array_map function

In addition to the array_column function, you can also use the array_map function to extract columns from a two-dimensional array. The following code is an example:

$students = array( 
array('name'=>'Alice', 'age'=>18, 'score'=>89), 
array('name'=>'Bob', 'age'=>19, 'score'=>92), 
array('name'=>'Cathy', 'age'=>18, 'score'=>91), 
array('name'=>'David', 'age'=>20, 'score'=>87), 
); 

$scores = array_map(function($student) { 
    return $student['score']; 
}, $students); 

print_r($scores);

The above code uses the array_map function to take out the score column of all elements in the two-dimensional array $students and form it into a new array $scores. Among them, the first parameter is the callback function, and the second parameter is the array to be operated on.

It should be noted that the efficiency of the array_map function is relatively low and may cause performance problems when processing large-scale arrays.

Among several ways to retrieve columns, the most commonly used method is to use the array_column function. In addition to fetching columns, the array_column function can also fetch the values ​​of specified columns to form a new array, and can use another index column as the key of the new array. These functions are very practical and can improve efficiency in actual development.

The above is the detailed content of How to get columns from php two-dimensional array. 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