如何将变量插入PHP数组? [英] How do I insert a variable into a PHP array?

查看:304
本文介绍了如何将变量插入PHP数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上寻找一些答案,但是没有一个是非常准确的.

I have looked for some responses on the web, but none of them are very accurate.

我希望能够做到这一点:

I want to be able to do this:

$id = "" . $result ["id"] . "";
$info = array('$id','Example');

echo $info[0];

这有可能吗?

推荐答案

您需要的是(不推荐):

What you need is (not recommended):

$info = array("$id",'Example'); // variable interpolation happens in ""

或者只是

$info = array($id,'Example'); // directly use the variable, no quotes needed

您已将变量括在单引号内,并且不会在单引号内进行变量插值,并且'$id'被视为长度为三的字符串,其中第一个字符为美元.

You've enclosed the variable inside single quotes and inside single quotes variable interpolation does not happen and '$id' is treated as a string of length three where the first character is a dollar.

这篇关于如何将变量插入PHP数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆