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 Opencv配置出错
  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?