dqsvf28682 2012-07-19 11:10
浏览 36
已采纳

我如何只访问第二个数组?

I'm doing this:

$sql_glassware = 'SELECT id, name FROM glassware';
$qry_glassware = $con->query($sql_glassware);
$get_glassware = $qry_glassware->fetchAll(PDO::FETCH_ASSOC);
debug($get_glassware);

debug() is a personal function; It returns the result like this:

$get_glassware = array(19) {
    [0]=>array(2) {
        ["id"]=>string(1) "1"
        ["name"]=>string(8) "Cocktail"
    }
    [1]=>array(2) {
        ["id"]=>string(1) "2"
        ["name"]=>string(9) "Margarita"
    }
    [2]=>array(2) {
        ["id"]=>string(1) "3"
        ["name"]=>string(8) "Highball"
    }
    ...
}

I'm guessing the first array level is the rows, and the second level is the columns.
don't know why it return the id as strings thoug...

Then I'm using a class to construct a complete form; I have a public function called addSelect() where the first argument takes an array of values to build the options list: array('name0','name1', 'name2','...') and do a foreach()-loop inside:

public function addSelect($opt=array(),$param=array())
    $name = $this->useEither($param['name'],'dropdown-list');  //  useEither() is a personal function
    foreach ($opt as $val => $name){
        $options .= '<option value="'.$val.'">'.$name.'</option>';
    }
    $select = '<select name="'.$name.'" '.$param['string'].'>'.$options.'</select>';
    $this->formElements[] = $select;  //  store the list for use later
}

How can re-write this litte function so I can pass $get_glassware directly into my function first argument and have it output the options like this:

<option value="1">Cocktail</option>
<option value="2">Margarita</option>
<option value="3">Highball</option>
  • 写回答

2条回答 默认 最新

  • dttphb59319 2012-07-19 11:47
    关注

    Try this:

    public function addSelect($opt=array(),$param=array())
        $name = $this->useEither($param['name'],'dropdown-list');  //  useEither() is a personal function
        foreach ($opt as $option){
            $options .= '<option value="'.$option['id'].'">'.$option['name'].'</option>';
        }
        $select = '<select name="'.$name.'" '.$param['string'].'>'.$options.'</select>';
        $this->formElements[] = $select;  //  store the list for use later
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比