首页 >后端开发 >php教程 >return多个值的引用方法

return多个值的引用方法

WBOY
WBOY原创
2016-06-06 20:26:251417浏览

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

<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>

回复内容:

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

<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>

还可以是字典,

<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>

<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>

使用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>
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn