【问题标题】:Select from SQL single row and set to php array [duplicate]从 SQL 单行中选择并设置为 php 数组 [重复]
【发布时间】:2019-12-07 20:57:20
【问题描述】:

我想选择一个单行名称news_images,其中包含这样的记录:

name.jpg,image.jpg,randomimageframe.jpg

这些图片没有相似的名称,所以我想将它们设置为数组,这是我的代码:

$info = array($row['news_image']);
print_r($info);
echo  $info[0] . ", " . $info[1] . " and " . $info[2] . ".";

我有错误:

数组 ( [0] => name.jpg,image.jpg,randomimagerame.jpg ) 注意:未定义的偏移量:D:\PORTAL\htdocs\KARGI\news.php 第 84 行中的 1

注意:未定义的偏移量:第 84 行 D:\PORTAL\htdocs\KARGI\news.php 中的 2 name.jpg、image.jpg、randomimageframe.jpg 和 .

我怎样才能做到这一点?我希望结果是这样的:

$info[0] = name.jpg
$info[1] = image.jpg
$info[2] = randomimagerame.jpg

【问题讨论】:

  • 不要将分隔符分隔的列表存储到数据库中!阅读“db normalization”。
  • 感谢您的回答,我会更改它。
  • 我会改变它...完美!你会过上更轻松的生活:)

标签: php arrays


【解决方案1】:

您可以使用explodeexplode()

 $info = explode(',',$row['news_image']);
 print_r($info);

【讨论】:

    猜你喜欢
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    相关资源
    最近更新 更多