分割数组

发布于 2024-11-19 11:36:32 字数 1799 浏览 0 评论 0原文

我正在尝试从数组中分割图像名称。

$this->_filenames[]= array($name);
$files = explode(",", $name);

当我对 $files 执行 var_dump 时,它会返回存储在数组中的两个图像,如下所示:

array
    0 => string 'felton300.jpg' (length=13)
1
array
    0 => string 'felton400.jpg' (length=13)
1 

如果我尝试将每个元素(使用下面的代码)存储在数组中使用 list() 我收到错误。

list($img1,$img2) = $files;

错误是:未定义的偏移量:1

我正在从表单上传图像,并希望使用变量(即 $img1)作为插入到 mysql 数据库的引用。这是我上传图像的代码。

public function move($overwrite = false) {
$field = current($this->_uploaded);
if(is_array($field['name'])){
foreach($field['name'] as $number => $filename){

//处理多重上传

$this->_renamed = false;
$this->processFile($filename,$field['error'][$number],
$field['size'][$number],
$field['type'][$number],$field['tmp_name'][$number],
$overwrite); 
}
} else  {
$this->processFile($field['name'],$field['error'],
$field['size'],$field['type'],$field['tmp_name'],$overwrite);
}
}

protected function processFile($filename,$error,$size, $type,$tmp_name,$overwrite){
$OK = $this->checkError($filename,$error);
if ($OK) {
$sizeOK = $this->checkSize($filename, $size);
$typeOK = $this->checkType($filename, $type);
if ($sizeOK && $typeOK) {
$name = $this->checkName($filename, $overwrite);

$success = move_uploaded_file($tmp_name, $this->_destination . $name); 
if ($success) {

//将修改后的文件名添加到文件名数组中

$this->_filenames[]= array($name);

//拆分数组***这是我想要拆分数组的位置,以便可以将图像存储在 MYSQL 表中。该表将有 3 列 id、image1、image2 ******

$files = explode(",", $name);
var_dump($files);

I'm trying to split the image names from an array.

$this->_filenames[]= array($name);
$files = explode(",", $name);

When I do a var_dump on $files it returns both images stored in the array as shown below:

array
    0 => string 'felton300.jpg' (length=13)
1
array
    0 => string 'felton400.jpg' (length=13)
1 

if I try to store each element (using the code below) in the array using list() I get an error.

list($img1,$img2) = $files;

The error is: Undefined offset: 1

I'm uploading the images from a form and want to use the variable (ie $img1) as a reference to insert into a mysql database. Here's my code for uploading the image.

public function move($overwrite = false) {
$field = current($this->_uploaded);
if(is_array($field['name'])){
foreach($field['name'] as $number => $filename){

//process the multiple upload

$this->_renamed = false;
$this->processFile($filename,$field['error'][$number],
$field['size'][$number],
$field['type'][$number],$field['tmp_name'][$number],
$overwrite); 
}
} else  {
$this->processFile($field['name'],$field['error'],
$field['size'],$field['type'],$field['tmp_name'],$overwrite);
}
}

protected function processFile($filename,$error,$size, $type,$tmp_name,$overwrite){
$OK = $this->checkError($filename,$error);
if ($OK) {
$sizeOK = $this->checkSize($filename, $size);
$typeOK = $this->checkType($filename, $type);
if ($sizeOK && $typeOK) {
$name = $this->checkName($filename, $overwrite);

$success = move_uploaded_file($tmp_name, $this->_destination . $name); 
if ($success) {

//add the amended filename to the array of filenames

$this->_filenames[]= array($name);

//split the array***THIS IS WHERE I WANT TO SPLIT THE ARRAY SO I CAN STORE THE IMAGES IN A MYSQL TABLE. THE TABLE WILL HAVE 3 COLUMNS id, image1, image2 ******

$files = explode(",", $name);
var_dump($files);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日暮斜阳 2024-11-26 11:36:32

如果数组中有两个图像,为什么不

INSERT INTO table (id, image1, image2) VALUES ({$id}, '{$files[0]}', '{$files[1]}')

麻烦地拆分它们呢?

从这个看起来

array
    0 => string 'felton300.jpg' (length=13)
1
array
    0 => string 'felton400.jpg' (length=13)
1 

你有一个多维数组(注意每个都有一个 0 的键)。很难判断您提供的内容,但

INSERT INTO table (id, image1, image2) VALUES ({$id}, '{$files[0][0]}', '{$files[0][1]}')

也可能是您的解决方案。

If you have two images in an array, why not

INSERT INTO table (id, image1, image2) VALUES ({$id}, '{$files[0]}', '{$files[1]}')

Why bother splitting them?

And from the looks of this

array
    0 => string 'felton300.jpg' (length=13)
1
array
    0 => string 'felton400.jpg' (length=13)
1 

You have a multidimensional array here (notice each has a key of 0). It's hard to tell with what you've provided, but

INSERT INTO table (id, image1, image2) VALUES ({$id}, '{$files[0][0]}', '{$files[0][1]}')

May also be a solution for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文