Php 使用变量调用数组项

Php 使用变量调用数组项,php,arrays,variables,Php,Arrays,Variables,是否可以使用变量调用数组项 $files = array('img/1.jpg','img/2.jpg','img/3.jpg'); $slot=0; $fullpath = 'img/4.jpg'; //In the original Programm this path is generated $files[$slot] = $fullpath; 当我尝试这段代码时,它不起作用 $files = array('img/1.jpg','img/2.jpg','img/3.jpg'); $s

是否可以使用变量调用数组项

$files = array('img/1.jpg','img/2.jpg','img/3.jpg');
$slot=0;
$fullpath = 'img/4.jpg'; //In the original Programm this path is generated
$files[$slot] = $fullpath;
当我尝试这段代码时,它不起作用

$files = array('img/1.jpg','img/2.jpg','img/3.jpg');
$slot=0;
$fullpath = $files[$slot];
并添加新值:

$new_value = 'img/4.jpg';
$files[] = $new_value; //or $files[] = 'img/4.jpg';
替换$files[$slot]

$fullpath = 'img/4.jpg';
$files[$slot] = $fullpath;
试试这个

$files = array('img/1.jpg','img/2.jpg','img/3.jpg');
$slot = 0;
if(isset($files[$slot]))
{
   $fullpath = realpath($files[$slot]);
}
为赋值切换变量


因为您正在为
$fullpath
变量赋值。

我确信它工作正常

<?php
$files = array('img/1.jpg','img/2.jpg','img/3.jpg');
$slot=0;
$files[$slot] = '';
array_push($files ,$fullpath);
?>


$fullpath未在代码段中定义,我猜您希望执行
$fullpath=realpath($files[$slot])像$fullpath=$files[$slot]那样执行;抱歉,这只是代码的一部分。让我编辑它,这样它才有意义。但是在这个解决方案中,$fullpath变量获取您编辑文章的$files[0]的值。。所以,您需要将$files[0]的值替换为$fullpath值o$fullpath变量?或者您需要在数组中获取值为$fullpath的索引?
<?php
$files = array('img/1.jpg','img/2.jpg','img/3.jpg');
$slot=0;
$files[$slot] = '';
array_push($files ,$fullpath);
?>