Home > Backend Development > PHP Tutorial > Which foreach method is better for processing list data in PHP?

Which foreach method is better for processing list data in PHP?

WBOY
Release: 2016-07-06 13:52:59
Original
1060 people have browsed it

In development, it is often necessary to process list data taken from the database,

There are three writing methods I have seen below, all of which can get the same result. I am always accustomed to using the first one.

Which one is better in terms of readability and performance?

List data such as:

<code>$list = [
    [
        'uid'=>100,
        'username'=>'test100',
        'nickname'=>'php_1',
        'gender'=>1,
    ],
    [
        'uid'=>200,
        'username'=>'test200',
        'nickname'=>'php_2',
        'gender'=>2,
    ],
    [
        'uid'=>300,
        'username'=>'test300',
        'nickname'=>'php_3',
        'gender'=>0,
    ],
];</code>
Copy after login
Copy after login

Writing method 1, modify in the original array:

<code>foreach($list as $k=>$v){
    if($v['gender']==0){
        $list[$k]['gender']='妖人';
    }else{
        $list[$k]['gender']='人';
    }
}</code>
Copy after login
Copy after login

Writing method 2, modify the original array, use reference:

<code>foreach($list as &$v){
    if($v['gender']==0){
       $v['gender']='妖人';
    }else{
       $v['gender']='人';
    }
}</code>
Copy after login
Copy after login

Writing 3, define a new array:

<code>
$data = [];
foreach($list as $k=>$v){
    if($v['gender']==0){
        $v['gender']='妖人';
    }else{
        $v['gender']='人';
    }
    $data[] = $v;
}</code>
Copy after login
Copy after login

Reply content:

In development, it is often necessary to process list data taken from the database,

There are three writing methods I have seen below, all of which can get the same result. I am always accustomed to using the first one.

Which one is better in terms of readability and performance?

List data such as:

<code>$list = [
    [
        'uid'=>100,
        'username'=>'test100',
        'nickname'=>'php_1',
        'gender'=>1,
    ],
    [
        'uid'=>200,
        'username'=>'test200',
        'nickname'=>'php_2',
        'gender'=>2,
    ],
    [
        'uid'=>300,
        'username'=>'test300',
        'nickname'=>'php_3',
        'gender'=>0,
    ],
];</code>
Copy after login
Copy after login

Writing method 1, modify in the original array:

<code>foreach($list as $k=>$v){
    if($v['gender']==0){
        $list[$k]['gender']='妖人';
    }else{
        $list[$k]['gender']='人';
    }
}</code>
Copy after login
Copy after login

Writing method 2, modify the original array, use reference:

<code>foreach($list as &$v){
    if($v['gender']==0){
       $v['gender']='妖人';
    }else{
       $v['gender']='人';
    }
}</code>
Copy after login
Copy after login

Writing 3, define a new array:

<code>
$data = [];
foreach($list as $k=>$v){
    if($v['gender']==0){
        $v['gender']='妖人';
    }else{
        $v['gender']='人';
    }
    $data[] = $v;
}</code>
Copy after login
Copy after login

Use 1 more often
Although 2 is the simplest way to write, there are pitfalls in quoting under foreach
If you use the $v variable again outside of foreach, the last value of foreach may be changed. , of course it can be guaranteed that this problem will not occur if it is not used

array_map

I generally use the first way of writing
The second way of writing is prone to problems

The recommended way to quote is writing method 2

The second method is better. You have already foreached it. There is already a one-dimensional array, so why do you need to fetch it from the list?

select if(gender=0,'魔人','人') as gendername ……

Get used to the first method

Related labels:
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