Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么多维数组替换第一个键值的第一个字母?_Php_Multidimensional Array - Fatal编程技术网

Php 为什么多维数组替换第一个键值的第一个字母?

Php 为什么多维数组替换第一个键值的第一个字母?,php,multidimensional-array,Php,Multidimensional Array,我有一个多维数组,它的行为出乎意料,我想知道为什么会这样,以及是否有解决办法。似乎如果我在数组的第一个键中设置了一些东西,当我在数组的第二个键中声明一个值时,它将替换它的第一个字母。 (我不确定如何正确描述行为,但此代码应该有所帮助: <?php //Declare Array with first key and value $test['hello'] = "Hello There"; //Echo Value echo "HELLO TEST: ". $test['

我有一个多维数组,它的行为出乎意料,我想知道为什么会这样,以及是否有解决办法。似乎如果我在数组的第一个键中设置了一些东西,当我在数组的第二个键中声明一个值时,它将替换它的第一个字母。 (我不确定如何正确描述行为,但此代码应该有所帮助:

<?php
//Declare Array with first key and value
    $test['hello'] = "Hello There";

//Echo Value
    echo "HELLO TEST: ". $test['hello'] ;

//Declare Multidimensional Array using the first array key and a new key
    $test['hello']['jerk'] = "JERK!";

//Echo Values
    echo "<br/>HELLO TEST: ". $test['hello'] ;
    echo "<br/>JERK TEST : ". $test['hello']['jerk'];
?>

该代码输出如下:

你好测试:你好那里
你好测试:果冻在那里
冲击试验:J

我希望看到

你好测试:你好那里
你好测试:你好那里
挺举测试:挺举


因为它不是数组,而是字符串

$test['hello'] = array();
$test['hello']['jerk'] = "JERK!"

您正试图将存储在
$test['hello']
中的字符串视为数组。您将无法使
$test['hello']
同时保存一个字符串(“你好”)和另一个数组。

您正试图声明
$test['hello']
分为两种:字符串和数组。它只能是一种或另一种。

执行此操作:

$test['hello'] = "Hello There";
$test['hello']['jerk'] = "JERK!";
$test['hello']['jerk'] = "JERK!";
您声明
$test['hello']
包含一个字符串

$test['hello'] = array();
$test['hello']['jerk'] = "JERK!"

然后,这样做:

$test['hello'] = "Hello There";
$test['hello']['jerk'] = "JERK!";
$test['hello']['jerk'] = "JERK!";
您声明
$test['hello']
包含一个数组,不再是字符串

$test['hello'] = array();
$test['hello']['jerk'] = "JERK!"

您的
$test['hello']
只能包含一个内容



实际上,当您这样做时:

$test['hello'] = "Hello There";
$test['hello']['jerk'] = "JERK!";
$test['hello']['jerk'] = "JERK!";
由于
$test['hello']
包含字符串(而不是数组),我认为PHP将尝试访问以下条目:
$test['hello'][0]

0
是转换为整数的
jerk
字符串

$test['hello'][0]
表示
$test['hello']
中字符串的第一个字符
请参阅手册中关于这一点的内容

现在,您试图将整个字符串(
“JERK!”
)放在只有一个字符的位置,即现有字符串的第一个字符。该字符将被字符串的第一个字符
“JERK!”
覆盖



请稍后编辑:以下是完整的解释以及注释代码:

// Assign a string to $test['hello']
$test['hello'] = "Hello There";

//Echo Value
var_dump($test['hello']);

// Try to assign a string to $test['hello']['jerk']
$test['hello']['jerk'] = "JERK!";
// But $test['hello'] is a string, so PHP tries to make a string-access to one character
// see http://fr.php.net/manual/en/language.types.string.php#language.types.string.substr
// As 'jerk' is a string, it gets converted to an integer ; which is 0
// So, you're really trying to do this, here :
$test['hello'][0] = "JERK!";
// And, as you can only put ONE character where ($test['hello'][0]) there is space for only one ,
// only the first character of "JERK!" is kept.
// Which means that what's actually done is :
$test['hello'][0] = "J";

// Still echo the whole string, with the first character that's been overriden
var_dump($test['hello']);

// Same as before : here, you're only accessing $test['hello'][0]
// (which is the first character of the string -- the one that's been overriden)
var_dump($test['hello']['jerk']);
// Same as this :
var_dump($test['hello'][0]);

字符串中的字符可以是 通过指定来访问和修改 所需的从零开始的偏移量 使用 方括号,如中所示 $str[42]…非整数[索引]为 已转换为整数…仅第一个 指定字符串的字符为 用过

因此,为了回答您的问题。
$test['hello']
是一个字符串,因此索引字符串的规则将适用。因此,
$test['hello']['jerk']=“jerk!”;
相当于
$test['hello'][0]=“J”
,因为
'jerk'
的intval是
0
,并且只有第一个字符
“J”将使用指定字符串的
“JERK!”

之后,当回显
$test['hello']
时,您将引用整个字符串,现在它的第一个字符被
J
替换。回显
$test['hello']['jerk']
再次等同于回显
$test['hello'][0]
因为
'jerk'
的intval是
0
,根据索引字符串的规则,
$test['hello'][0]
将返回
$test['hello']
的第一个字符

在解释你的意图时,也许你想要这个

$test['hello'] = "Hello There";
$test['jerk'] = "JERK!";
print_r($test); // array('hello' => "Hello There", 'jerk' => "JERK!")
或者,有一些多维的东西

$test['message']['hello'] = "Hello There";
$test['message']['jerk'] = "JERK!";
print_r($test);
// array('message' => array('hello' => "Hello There", 'jerk' => "JERK!"))

值得指出的是,在给出的代码示例中发生了什么

可以像基于索引的数组一样访问字符串。当您尝试设置“数组”的第二级时,它实际上是这样做的(因为第一级是字符串):


基本上,这将给出(或设置)字符串的第一个字符,因为“jerk”转换为整数是0。如果您的字符串可以转换为不同的整数,那么它将返回(或设置)字符串的不同字符。

没错,$test['hello']在Mallow的代码中最初声明为字符串谢谢,这有助于我了解我能做什么:哦,我正要提交一个回答说这个。哦,好吧,你打败了我。谢谢,我喜欢收到所有这些回复,这是我第一次使用堆栈溢出,我无法跟上所有的回复哈哈,这太棒了!实际上我是试图为表单变量的各个方面分配不同的值,因此我设置了$formvalue['company']=$\u POST['company'],并希望减少发送给函数的参数量,这样我就可以执行$formvalue['company']['errormessage']和$formvalue['company']['elementname']='company'…我想我应该只做post value=$formvalue['company']['value']谢谢你的详细说明这很好,我不得不重新考虑我的设计:)@Mallow:不客气:-)(我编辑了我的答案,并做了一些解释)谢谢,我希望不会太麻烦:DIt不是:-)事实上,这是关于PHP的一些有趣的问题,它所做的事情并不总是显而易见的^^