duanbei1903
2016-07-10 14:16
浏览 381
已采纳

如何从数据库中获取逗号分隔值

I have the following mysqli query, and I want to implode the array that's outputted into a (1,2,3,4) type format.

Here's the query and the asoc array code:

$user_categories = mysqli_query($connect, "SELECT sub_cat FROM subscriptions WHERE sub_user_id = '$user_id'");

$category_ids = mysqli_fetch_all($user_categories,MYSQLI_NUM);

print_r($category_ids);

$category_ids = implode(", ",$category_ids);

I then get the following output, and I can't seem to isolate the values...

Array ( 
[0] => Array ( [0] => 5 ) 
[1] => Array ( [0] => 8 ) 
[2] => Array ( [0] => 4 ) 
[3] => Array ( [0] => 2 ) 
)

Apologies if I'm missing something really obvious here. I've been trying to fix this for a while, and due to my lack of PHP experience, I'm not 100% sure what to search for.

I've also tried a simple implode using the results of the $user_categories query, following the instructions of other StackExchange answers I've seen on the topic, but got nothing (code below):

$user_categories = mysqli_query($connect, "SELECT sub_cat FROM subscriptions WHERE sub_user_id = '$user_id'");

$category_ids = implode(", ",$user_categories);

echo $category_ids;

图片转代码服务由CSDN问答提供 功能建议

我有以下mysqli查询,我想内联输出到的数组(1, 2,3,4)类型格式。

这是查询和asoc数组代码:

  $ user_categories =  mysqli_query($ connect,“SELECT sub_cat FROM subscriptions WHERE sub_user_id ='$ user_id'”); 
 
 $ category_ids = mysqli_fetch_all($ user_categories,MYSQLI_NUM); 
 
print_r($ category_ids); 
 
 $ category_ids  = implode(“,”,$ category_ids); 
   
 
 

然后我得到以下输出,我似乎无法隔离这些值...

 数组(
 [0] =>数组([0] => 5)
 [1] =>数组([0] => 8)  
 [2] =>数组([0] => 4)
 [3] =>数组([0] => 2)
)
   \  n 
 

道歉,如果我遗漏了一些非常明显的东西。 我一直试图解决这个问题,由于我缺乏PHP经验,我不能100%确定要搜索什么。

我也试过了 使用 $ user_categories 查询的结果进行简单的内爆,遵循我在主题上看到的其他StackExchange答案的说明,但什么都没有(下面的代码): < pre> $ user_categories = mysqli_query($ connect,“SELECT sub_cat FROM subscriptions WHERE sub_user_id ='$ user_id'”); $ category_ids = implode(“,”,$ user_categories); echo $ category_ids;

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dstobkpm908182 2016-07-10 14:26
    已采纳

    $category_ids is an array of arrays (rows), so you can't just implode it. You need to fetch the first value from each row and implode that.

    PHP 5.5+ solution:

    Using array_column():

    $category_ids = implode(', ', array_column($category_ids, 0));
    
    echo $category_ids;
    

    Output:

    5, 8, 4, 2

    PHP 5.3+ solution:

    Subtitute array_map() for array_column():

    $category_ids = implode(', ', array_map(function ($row) { return $row[0]; }, $category_ids));
    
    echo $category_ids;
    

    Output:

    5, 8, 4, 2

    点赞 评论
  • dongmieqiao3152 2016-07-10 14:28

    $category_ids that you get from mysqli_fetch_all is array of arrays and the desired result cannot be retrieved just by passing it to implode. However, this should do the job:

    $category_ids = mysqli_fetch_all($user_categories,MYSQLI_NUM);
    
    $category_ids_imploded = implode(', ', array_map(function ($entry) {
      return $entry['0'];
    }, $category_ids));
    

    Alternatively, in case you're using PHP 5.5 or higher, you can make it a bit less messy by using array_column:

    $category_ids_imploded = implode(', ', array_column($category_ids, 0));
    
    点赞 评论
  • douhe6181 2016-07-10 15:26

    Mysql solution

    $sql = "SELECT group_concat(sub_cat) FROM subscriptions WHERE sub_user_id = '$user_id'"
    $result = mysqli_query($connect, $sql);
    $category_ids = mysqli_fetch_row($user_categories)[0];
    

    PDO solution (preferred)

    $sql = "SELECT group_concat(sub_cat) FROM subscriptions WHERE sub_user_id = ?"
    $stmt = $connect->prepare($sql);
    $stmt->execute([$user_id]);
    $array = $stmt->fetchAll(PDO::FETCH_COLUMN);
    $category_ids = implode(",", $array);
    
    点赞 评论

相关推荐 更多相似问题