简体   繁体   English

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

[英]How to fetch multiple row value which contains multiple value separated by comma into a single array in php using pdo mysql

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.现在我需要使用 pdo php 将多行中的值提取到一个数组中。

在此处输入图片说明

<?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, like this:但我需要一个数组中的所有值,如下所示:

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

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.如果有重复,他们会显示。

You can also replace your你也可以更换你的

$d[] = explode(",",$value['position']);

with

array_merge($d, explode(',', $value['position']));

That will make one array rather than an array of arrays.这将创建一个数组而不是一组数组。 More info on array_merge at php.net. php.net 上有关array_merge 的更多信息。

A lot of PHP functions are more performant than the equivalent written in PHP.许多 PHP 函数的性能比用 PHP 编写的等效函数要高。 Ie this may be faster than a nested foreach loop.即这可能比嵌套的foreach循环更快。 As always, if this is important to you, time both ways.与往常一样,如果这对您很重要,请双向计时。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP:将带有多个数组值的分隔逗号字符串值插入MySql - PHP: Insert separated comma string value with as multiple array value Into MySql 如何从MySql中具有多个值(逗号分隔)的列中获取整数值 - How to fetch integer value from column having multiple values (comma separated) in MySql php:如何从逗号(,)分隔的数组中获取单个值 - php: how to get single value from a comma(,) separated array 如何从PHP / MYSQL中的单个表中使用特定值获取包含多个值的多个列 - how to fetch multiple columns containing multiple values using a specific value from single table in PHP/MYSQL 如何在MySQL的单行中获取以逗号分隔格式存储的图像? - How to fetch images that stored in comma separated format in single row in MySQL? 如何使用JavaScript将多个下拉列表选择的值提取到单个数组中 - How to fetch multiple dropdowns selected value into a single array using JavaScript 如何在多个键值中转换单个逗号数组值 - How to convert the single comma array value in multiple key value 如何编写查询以通过逗号将不同行的列值分隔为php中的单行 - How to write a query to get different row's column value by comma separated into a single row in php 如何使用PHP,Angular.js和MySQL由单个用户提交的多行插入值 - How to insert value in multiple row by a single user submit using PHP,Angular.js and MySQL 选择值在列中的行,其中多个数据用逗号分隔 - Select row where value is in column with multiple data separated comma
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM