douwei8672 2019-01-07 05:24
浏览 121
已采纳

如何使用pdo mysql将包含多个由逗号分隔的值的多个行值提取到php中的单个数组中

I have a database table name books.Now i need to fetch value from multiple row values (column name position) which contains multiple value separated by comma.Now i need to fetch values from multiple rows into an array using pdo php. Thanks in advanced
enter image description here

<?php
require_once "includes\config.php";

$sql = 'SELECT * FROM books WHERE shelf_no=2';
$stmt = $db->prepare($sql);
$stmt->execute();
$row = $stmt->fetchALl(PDO::FETCH_ASSOC);
$d = array();
foreach ($row as $value) {
$d[] = explode(",",$value['position']);
}

print_r($d);
?>

and the result is

Array (
  [0] => Array (
    [0] => b1 
    [1] => b2 
  ) 
  [1] => Array ( 
    [0] => a2 
    [1] => a3
  )
)

but i need all values in a single array

  • 写回答

2条回答 默认 最新

  • donoworuq450547191 2019-01-07 06:47
    关注

    You are exploding it (creating a new array) and then adding that array to another array.

    You could run through each value in the exploded string and add them:

    <?php
    require_once "includes\config.php";
    
    $sql = 'SELECT * FROM books WHERE shelf_no=2';
    $stmt = $db->prepare($sql);
    $stmt->execute();
    $row = $stmt->fetchALl(PDO::FETCH_ASSOC);
    $d = array();
    foreach ($row as $value) {
        $temp = explode(",",$value['position']);
        foreach($temp as $pos){
            array_push($d, trim($pos));
        }   
    }
    
    print_r($d);
    ?>
    

    If there are duplicates they will show though.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python