[英]array indexing not working properly
Hey guys i have tried to add the key of an array to another array's key but i didnt get the output as i expected ..My code is 大家好,我尝试将一个数组的键添加到另一个数组的键中,但是我没有得到预期的输出..我的代码是
<?php
$some = array('anna'=>'2','revathy'=>'baba');
$honey = array_keys($some);
$something = array();
foreach($honey as $value) {
$something[$value]=$some[$value];
echo $something[$value];
}
?>
When i run this code it shows blank screen instead of the output ..Hope you guys can help me out ..Any help would be appreaciated.. 当我运行此代码时,它将显示黑屏而不是输出..希望你们可以帮助我..任何帮助都会得到帮助..
This is because array_keys
returns something like this: 这是因为
array_keys
返回如下内容:
array (size=2)
0 => string 'anna' (length=4)
1 => string 'revathy' (length=7)
So, what you need to do to make your script work is like this: 因此,需要执行以下操作才能使脚本正常工作:
ps in your script you had an undefined variable $v
ps在脚本中,您有未定义的变量
$v
<?php
$some = array('anna'=>'2','revathy'=>'baba');
$honey = array_keys($some);
$something = array();
foreach($honey as $key => $value) // this is the change
{
$something[$value] = $some[$value];
echo $something[$value] ."<br>"; // and here
}
Outputs: 输出:
2
baba
foreach ($honey as $key => $value) {
$something[$key]=$value;
}
This is the proper way of dong this :) There is absolutely no need for $honey = array_keys($some); 这是解决此问题的正确方法:)绝对不需要$ honey = array_keys($ some);
You don't need to do that to copy an array with php. 您无需执行此操作即可使用php复制数组。 The equal operator copies it unless you use the reference operator (
= &$some
). 除非您使用引用运算符(
= &$some
),否则等于运算符将复制它。
The only thing you have to do is : 您唯一要做的是:
<?php
$some = array('anna'=>'2','revathy'=>'baba');
$something = $some;
?>
And to additionally print it : 并另外打印:
echo implode(', ', array_values($something));
将$ v更改为$ value,对我有用
you can skip 你可以跳过
$honey = array_keys($some);
just do 做就是了
foreach($some as $key => $value)
{
$something[$key] = $value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.