php 数组 索引 变量,PHP数组索引使用变量(PHP Array Index using variable)

PHP数组索引使用变量(PHP Array Index using variable)

在使用声明的变量作为我的数组索引时,我在获取数组内容时遇到问题。

lxWDf.png

$indexes = "[0][1][0][1]";

$code = $params["smv_code"].$indexes;

这将返回“数组到字符串转换错误”;

请注意,$ indexes是动态的,取决于数组索引的“parent_0_1_0_1”参数内容。

I'm having a trouble getting my array content when using a declared variable as my array indexes shown below.

lxWDf.png

$indexes = "[0][1][0][1]";

$code = $params["smv_code"].$indexes;

this returns an "Array to string conversion error";

Note that the $indexes is dynamic depends on the "parent_0_1_0_1" params content of the array index.

原文:https://stackoverflow.com/questions/45929872

2019-12-22 16:55

满意答案

这个. 运算符只是进行字符串连接 - 它不适用于那样的代码。 我不认为PHP能够像这样解释原始代码 - 至少不安全。 (您可以始终使用eval ,但使用类似的东西存在严重的安全问题 - 注入等)。

我建议你自己通过解释索引来“手动”遍历数组。 将$indexes更改为"0,1,0,1"并执行以下操作:

$index_array = explode(',',$indexes)

$code = $params["smv_code"];

foreach($index_array as $i) {

$code=$code[$i];

}

$code应该是您正在寻找的价值。

the . operator just does a string concatenation - it's not going to work for code like that. I don't think PHP has the ability to interpret raw code like that - at least not safely. (You can always use eval, but there are serious security concerns for using something like that - injection, etc.).

I would suggest just traversing the array "manually" through interpreting the indexes yourself. Change the $indexes to "0,1,0,1" and do the following:

$index_array = explode(',',$indexes)

$code = $params["smv_code"];

foreach($index_array as $i) {

$code=$code[$i];

}

$code at the end should be the value you're looking for.

2017-08-29

相关问答

只需删除$id["1"]周围的撇号: $output[$id["1"]]

否则, '$id["1"]'被字面上视为字符串索引。 Just remove the apostrophes around $id["1"]: $output[$id["1"]]

Otherwise, the '$id["1"]' is treated literally as a string index.

您可以在JS中定义arrayName变量,并使用服务器中的值对其进行初始化: var arrayName = <?php echo json_encode($arrayName); ?>;

$(".elementClass").each(function(index, id) {

$(id).html(arrayName[index-1]);

});

You can define arrayName variable in JS and initialize it with the va...

Mate,你的脚本没有任何错误! 实际上这正是预期的行为。 当您从浏览器访问该页面时,将设置一个特定的COOKIE ,并且$_SESSION数组将绑定到此cookie。 当您使用file_get_contents($url2, true);访问该页面时file_get_contents($url2, true); 服务器正在为另一个COOKIE分配全新的$_SESSION数组绑定。 显然第二个$_SESSION数组没有map键设置,所以正如你所期望的那样 Notice: Undefined ind...

http://sandbox.onlinephpfunctions.com/code/c5acd07edcc910bfdd80a27bee0583d8540e17e3 这是100000次的结果: First: 0.0018520355224609 with a pointer to the actual value

Second: 0.003324031829834 with a pointer to the array

就像你可以看到有一个很大的不同,但现在计算机要快速,甚至让你的时间值得担...

以下是使用jQuery $.post方法的示例: $(document).ready(function(){

var my_var;

$.post("my-php-page.php",

{my_index: 1},

function(data) {

my_var = data.my_value;

},

"json");

});

我的PHP-page.php文件 <?php

$phpArray[...

这个. 运算符只是进行字符串连接 - 它不适用于那样的代码。 我不认为PHP能够像这样解释原始代码 - 至少不安全。 (您可以始终使用eval ,但使用类似的东西存在严重的安全问题 - 注入等)。 我建议你自己通过解释索引来“手动”遍历数组。 将$indexes更改为"0,1,0,1"并执行以下操作: $index_array = explode(',',$indexes)

$code = $params["smv_code"];

foreach($index_array as $i) {

$...

您可以使用empty()来检查条目是否为空: if (empty($vars['job_role'])) {

echo "Job role can't be empty";

} else {

echo "Job role submitted";

}

因为isset()检查定义的值(被设置),而不是空的。 在你的代码中, $vars['job_role']可能是一个空字符串,所以它被定义了,但是它是空的。 You can use empty() to check if the en...

你的数组看起来像这样: $arr = Array(

'first' => array(

'val' => 100

),

'second' => array(

'val' => 200

),

'thirth' => array(

'val' => 300

),

);

您可以像这样访问它们: echo $arr['first']['val']; // 100

像这样循环他们: foreach($arr as $val){...

echo ${"array_$array_numerals[$i]"}[$j];

但我必须警告你,这样做是一个非常糟糕的主意。 而是使用另一个查找数组: $array = Array(

0 => Array(

2 => "$100",

...

),

...

);

echo $array[$i][$j];

echo ${"array_$array_numerals[$i]"}[$j];

But I must warn you that it is a very b...

+添加数字。 . 连接字符串。 echo "Hello " . "world!";

+ adds numbers. . concatenates strings. echo "Hello " . "world!";

相关文章

这是我在lucene in action 中看到的,本来想翻译一下,但是翻译成汉语就没有原来的味道了。

...

http://www.1990c.com/?p=932 近在做微信公众平台开发,一口气写了二十几个功能

...

需求是导入数据到MongoDB. 1创建目录 在$Nutch_home/src/plugin

...

php如何实现调用搜索引擎接口;这里有两个方案; 一:PHP自带的curl,当然首先要开启curl,这

...

Solr PHP support Contents Solr PHP support

...

背景:项目中需要将原有的sphinx搜索引擎换成solr,以下是通过参照网络内容后,从搭建到调用的一次

...

php中文字符串长度及定长截取问题使用str_len("中国") 结果为6,php

...

使用SolrJ操作Solr会比利用httpClient来操作Solr要简单。SolrJ是封装了http

...

Solr在启动时,通过多线程的方式加载core,在加载完每个core的配置文件后,实例化了一个Solr

...

Solr4.8.0源码分析(3)之index的线程池管理 Solr建索引时候是有最大的线程数限制的,

...

最新问答

如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re

第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型

这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;

问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是

我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar

Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/

你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV

12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar

这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值