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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测