php - 为什么我不能在 PHP 类的嵌套数组中附加元素?
问题描述
我有一个包含二维数组的 PHP 类:
class ArrApp
{
private $cms = [
'S' => 'A',
'V' => []
];
...
}
该类具有在内部数组中附加指定元素的方法:
public function App($elm)
{
$V = $this->cms[0]['V'];
array_push($V, $elm);
$this->Prt();
}
Prt() 方法只是打印外部数组:
public function Prt()
{
print_r($this->cms);
echo '<br>';
}
我实例化该类并尝试在内部数组中附加一个元素:
$aa = new ArrApp();
$aa->Prt();
$aa->App(1);
$aa->Prt();
但是,O/P 显示一个空的内部数组:
数组 ( [S] => A [V] => 数组 ( ) )
数组 ( [S] => A [V] => 数组 ( ) )
数组 ( [S] => A [V] => 数组 ( ) )
- 为什么会这样?它与“按值传递”/“按引用传递”问题有关吗?
- 我该如何解决?
解决方案
您尝试访问关联数组中的元素 0,但您不能这样做。
public function App($elm) {
$V = $this->cms[0]['V']; // here
推荐阅读
- python-3.x - 如何在数据帧中迭代的for循环中调用函数?
- ios - 如何限制使用 64 位设备的用户安装我的 iOS 应用程序?
- spring - 引起:org.springframework.dao.IncorrectResultSizeDataAccessException:不正确的结果大小:Spring Data Redis
- amazon-s3 - Using S3 to group SQS messages instead of FIFO queues
- python - IndexError: 范围 [1, 3) 中的预期暗淡 0 索引) 错误 Python
- reactjs - React + Jest 单元测试中的 Material-UI 排版错误
- javascript - 动态输入字段的搜索功能
- ruby-on-rails - 如何在rails中为编辑页面生成过期链接
- google-cloud-platform - 您如何为 Google Cloud 平台签署 HIPAA BAA?
- concurrency - 并发线程组在jmeter中具有相同配置的结果中具有不同的总样本