欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

return多个值的引用方法

程序员文章站 2024-01-25 11:55:10
...
输出指定的值,除了索引方式还有其它方式吗?
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);
}

回复内容:

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

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);
}

还可以是字典,

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);
}


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

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

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);
}
相关标签: php