【问题标题】:PHP Array to string conversion in multiple selected dropdown多个选定下拉列表中的 PHP 数组到字符串转换
【发布时间】:2015-09-09 04:01:34
【问题描述】:

我有这个功能可以在多个下拉列表中显示选定的值:

function _is_dropdown_( $name,array $selected=null,$table ,$class,$required,$type_name,$type, $size )
{
        $options = DB::fetch("SELECT name, id FROM " . $table . " WHERE ". $type_name ." = ? ORDER BY name",$type);
      //$options = array();
        /*** begin the select ***/
        $dropdown = '<select name="'.$name.'" id="'.$name.'" class="'.$class.'" required = "'.$required.'" size="'.$size.'" multiple>'."\n";

        /*** loop over the options ***/
        foreach($options as $key=>$option )
        {
                /*** assign a selected value ***/
                $select = in_array( $option, $selected ) ? ' selected' : null;

                /*** add each option to the dropdown ***/
                $dropdown .= '<option value="'.$key.'" '.$select.' >'.$option.'</option>'."\n";
        }

        /*** close the select ***/
        $dropdown .= '</select>'."\n";

        /*** and return the completed dropdown ***/
        return $dropdown;
}

结果:

$name = 'book_cover[]';
$selected = json_decode($DB_QUERY[0]['book_cover'] , true);
echo _is_dropdown_( $name,$selected ,NEWS_TYPE ,'contentgroup','required','book_type','4', '4' );

现在结果下拉列表中不显示option 的名称:

<select name="book_cover[]" class="contentgroup" required = "required" size="4" multiple>
<option value="0"  >Array</option>
<option value="1"  >Array</option>
<option value="2"  >Array</option>
<option value="3"  >Array</option>
<option value="4"  >Array</option>
<option value="5"  >Array</option>
<option value="6"  >Array</option>
<option value="7"  >Array</option>
<option value="8"  >Array</option>
<option value="9"  >Array</option>
<option value="10"  >Array</option>
<option value="11"  >Array</option>
<option value="12"  >Array</option>
</select>

并显示此错误:

[2015-09-09 08:28:06] [E_NOTICE] [8] Array to string conversion in C:\xampp\htdocs\cms\class\functions.php:1032 

编辑:我print_r$options:

(
    [0] => Array
        (
            [name] => test
        )

    [1] => Array
        (
            [name] => test2
        )

    [2] => Array
        (
            [name] => test3
        )
)

编辑 2:我将 $options 更改为手动数组:$options = array( 'test', 'test1', 'test2' ); 这工作正常。现在我需要将my db result 转换为此。

如何解决这个错误?!

【问题讨论】:

  • 显然$option 是一个数组。需要查看查询的结果集才能进一步调试。

标签: php arrays string


【解决方案1】:

这里你的键是 0,1,2... 值是

$option = Array
(
    [name] => test
)

所以用 $option["name"] 代替 $option。

foreach($options as $key=>$option ) {

      $select = in_array( $option["name"], $selected ) ? ' selected' : null;
      $dropdown .= '<option value="'.$key.'" '.$select.' >'.$option["name"].'</option>'."\n";

 }

【讨论】:

    【解决方案2】:

    你有 $options 是行。所以每个$option 是一行,一个包含两个元素(name, id) 的数组。

    【讨论】:

      【解决方案3】:

      更新了功能,请使用这个:

       function _is_dropdown_( $name,array $selected=null,$table ,$class,$required,$type_name,$type, $size )
          {
                  $options = DB::fetch("SELECT name, id FROM " . $table . " WHERE ". $type_name ." = ? ORDER BY name",$type);
                //$options = array();
                  /*** begin the select ***/
                  $dropdown = '<select name="'.$name.'" id="'.$name.'" class="'.$class.'" required = "'.$required.'" size="'.$size.'" multiple>'."\n";
      
                  /*** loop over the options ***/
                  foreach($options as $key=>$option )
                  {
                          /*** assign a selected value ***/
                          $select = ($key==$selected ? ' selected' : null );
      
                          /*** add each option to the dropdown ***/
                          $dropdown .= '<option value="'.$key.'" '.$select.' >'.$option[$key]['name'].'</option>'."\n";
                  }
      
                  /*** close the select ***/
                  $dropdown .= '</select>'."\n";
      
                  /*** and return the completed dropdown ***/
                  return $dropdown;
          }
      

      【讨论】:

      • &lt;b&gt;Parse error&lt;/b&gt;: syntax error, unexpected 'if' (T_IF) in
      • 不起作用。我将 $options 更改为手动数组:$options = array( 'test', 'test1', 'test2' ); 这工作正常。现在我需要将我的数据库结果转换为这个。
      【解决方案4】:

      检查一下

      $dropdown .= '<option value="'.$key.'" '.$select.' >'.$option['name'].'</option>'."\n";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-12
        • 1970-01-01
        • 1970-01-01
        • 2017-10-13
        • 2017-05-01
        相关资源
        最近更新 更多