Home  >  Article  >  Backend Development  >  How to remove several columns from a two-dimensional array in php

How to remove several columns from a two-dimensional array in php

PHPz
PHPzOriginal
2023-04-20 10:15:171014browse

In PHP programming, two-dimensional arrays are one of the most common data types. Two-dimensional arrays are composed of multiple one-dimensional arrays, which can facilitate multi-dimensional data storage and processing. In actual programming, we often need to take out several columns from a two-dimensional array and operate or output them. This article will introduce several methods to extract several columns from a two-dimensional array.

1. Use loop traversal

The simplest method is to use a loop to traverse the two-dimensional array and remove the required columns. The following is a sample code:

$array = array(
  array('id'=>1, 'name'=>'张三', 'age'=>18, 'address'=>'北京'),
  array('id'=>2, 'name'=>'李四', 'age'=>20, 'address'=>'上海'),
  array('id'=>3, 'name'=>'王五', 'age'=>22, 'address'=>'广州'),
);

$columns = array('name', 'age');

foreach($array as $key => $item){
  foreach($columns as $col){
    echo $item[$col]."  ";
  }
  echo "<br/>";
}

Here we define a two-dimensional array $array, which contains information such as each person's id, name, age and address. We need to extract the name and age column information from it. Use two foreach loops to traverse the array and the columns to be output. Through $item[$col], you can retrieve the corresponding information of each person and then output it directly.

2. Use array_column function

PHP’s array_column function can directly take out a certain column from a two-dimensional array without writing a loop yourself. The following is a sample code:

$array = array(
  array('id'=>1, 'name'=>'张三', 'age'=>18, 'address'=>'北京'),
  array('id'=>2, 'name'=>'李四', 'age'=>20, 'address'=>'上海'),
  array('id'=>3, 'name'=>'王五', 'age'=>22, 'address'=>'广州'),
);

$columns = array('name', 'age');

$result = array_column($array, $columns[0]);

for($i=1; $i<count($columns); $i++){
  $temp = array_column($array, $columns[$i]);
  $result = array_map(null, $result, $temp);
}

foreach($result as $value){
  echo $value[0]." ".$value[1]."<br/>";
}

Here we also define a two-dimensional array $array and the columns to be output $columns. By calling the array_column function, you can directly retrieve the values ​​of all name columns from $array, and then use the array_map function to merge the values ​​of multiple columns into a two-dimensional array $result. Finally, output each row of data in $result through the foreach loop.

3. Use the array_reduce function

Another implementation method is to use the array_reduce function of PHP. The array_reduce function combines an array into a value and is often used for operations such as summing arrays or calculating the maximum value. The following is a sample code:

$array = array(
  array('id'=>1, 'name'=>'张三', 'age'=>18, 'address'=>'北京'),
  array('id'=>2, 'name'=>'李四', 'age'=>20, 'address'=>'上海'),
  array('id'=>3, 'name'=>'王五', 'age'=>22, 'address'=>'广州'),
);

$result = array_reduce($array, function($carry, $item){
  return array($carry[0]." ".$item['name'], $carry[1]." ".$item['age']);
}, array('',''));

echo $result[0]."<br/>";
echo $result[1]."<br/>";

Here we also define a two-dimensional array $array. By calling the array_reduce function, the name and age information in each one-dimensional array $item are merged into a one-dimensional array $carry. Finally, output the merged result.

Summary:

The above three methods can remove the specified column from the two-dimensional array and process or output it. Among them, loop traversal is the most common method, and array_column and array_reduce functions are more concise and advanced. In actual programming, you can choose the most appropriate method to operate according to specific needs.

The above is the detailed content of How to remove several columns from a two-dimensional 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