php - 我如何在 php 中为字符串写这个并且不给我错误
问题描述
$array_names=array("phone","car","house");
我有这个字符串: $phone_[$jr]
但是带有字符串的字符串称为电话它是值的名称,我想使用其他名称来获取值,因此有必要这样写:
${$phone}
它需要在这个字符串中使用其他东西的问题:
${$phone}_[$jr]
这给了我错误
我看到了“_”的问题,但我需要使用,我的问题是关于我如何需要正确的工作
${$电话}_[$jr]
在其他情况下,我需要使用:
${$house}_[$jr]
ETC, .....
谢谢
解决方案
我假设你有这样的场景:
// this is the array you are trying to access dynamically
$bar_ = [];
// this will hold the first part of the variable name
$phone = 'bar';
// and this is how you would build the string which is the full name of the array
// You can then use it to access one of its elements.
${$phone.'_'}[1] = 'f';
推荐阅读
- javascript - 为什么我调用它时我的对象不显示?
- jvm - 使用 VisualVm 分析堆转储时出现内存不足错误
- java - 将嵌套的哈希映射写入文件并在 Java 中删除元素时出现 ConcurrentModificationException 错误
- multithreading - 为什么加入协程范围内的所有作业后线程名称会更改?
- regex - $dir =~ s/\n// 是什么意思?我需要帮助以简单易懂的方式解释它
- kubernetes - Statefulset 上的容器未在 minikube dns 上注册
- node.js - 定义 Mongoose 与现有遗留 MongoDB 模式的模式的关系
- java - 如何将文本字段传递给框架的标题?
- ios - NSNotificationCenter 永远不会执行
- android - 如何使用变量来调用不同的 r.color