Home > Backend Development > PHP Tutorial > return多个值的引用方法

return多个值的引用方法

WBOY
Release: 2016-06-06 20:26:25
Original
1428 people have browsed it

输出指定的值,除了索引方式还有其它方式吗?

<code>public function index(){
    $new = $this->index_com();
    //输出指定的值
    echo $new[2];
}

public function index_com(){
    $list = 'a';
    $category = 'b';
    $totalCount = 'c';
    $current = 'd';
    return array($list, $category, $totalCount, $current);
}
</code>
Copy after login
Copy after login

回复内容:

输出指定的值,除了索引方式还有其它方式吗?

<code>public function index(){
    $new = $this->index_com();
    //输出指定的值
    echo $new[2];
}

public function index_com(){
    $list = 'a';
    $category = 'b';
    $totalCount = 'c';
    $current = 'd';
    return array($list, $category, $totalCount, $current);
}
</code>
Copy after login
Copy after login

还可以是字典,

<code>public function index(){
    $new = $this->index_com();
    //输出指定的值
    echo $new[list];
}

public function index_com(){
    $list       = 'a';
    $category   = 'b';
    $totalCount = 'c';
    $current    = 'd';
    return array('list'=>$list, 'category'=>$category);
}</code>
Copy after login

<code class="php">
public function index(){
    $new = $this->index_com($k);
    //输出指定的值
    echo $new;
}
public function index_com($k){
    $arr=['list'=>a,'category'=>'c'];
    return $arr[$k];</code>
Copy after login

使用list语法可以达到你想要的效果:

<code>public function index(){
   list($list, $category, $totalCount, $current) = $this->index_com();
   //输出指定的值
   echo $category;
}

public function index_com(){
    $list = 'a';
    $category = 'b';
    $totalCount = 'c';
    $current = 'd';
    return array($list, $category, $totalCount, $current);
}</code>
Copy after login
Related labels:
php
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