【问题标题】:How to convert an SQL result string to an array in Codeigniter?如何将 SQL 结果字符串转换为 Codeigniter 中的数组?
【发布时间】:2016-11-20 15:30:37
【问题描述】:

我需要将 [图片名称] 字符串转换为数组。怎么做?

【问题讨论】:

  • 避免使用代码图像,将代码库作为<code>在问题中,explode()
  • 使用explode()函数
  • 您不需要在问题标题中插入标签,您的问题下方已经有标签云。这就是回答的人使用的。如果你给你的问题一个非常清晰、描述性、格式良好的标题,它会更有用。

标签: php mysql arrays string codeigniter


【解决方案1】:

在查询后添加这些行

$result = $this->db->get()->result_array();
foreach($result as $key=>$row) {
    $result[$key]['picture_names'] = explode(',',$row['picture_names']);
}

【讨论】:

    【解决方案2】:

    要将您的 $array1[picture_names] 转换为数组本身,您可以使用“explode()”函数。

    array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )
    

    即在您的具体示例中:

    $results = $this->db->get()->result_array();
    $exp_results = explode(',', $results);
    

    应该给出这样的数组:

    $exp_results = Array (
        0 => 21
        1 => 22
        2 => 25
        3 => 27
        4 => .... etc
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      • 2013-06-07
      • 1970-01-01
      相关资源
      最近更新 更多