PHP联合运算符输出是不可理解的

时间:2017-11-04 18:09:42

标签: php arrays

当我编写以下代码时,任何人都可以解释原因:

a = []

File.open('documents/programmi_test/verbi.txt') do |file|
  file.lines.each do |line|
    a << line.split.map(&:to_s)
  end
end

puts ''
b = rand(3)
puts a[b][0]
puts 'infinitive'

infinitive = gets.chomp
puts infinitive == a[b][1] ? 'simple past' : 'retry'
pastsimple = gets.chomp
puts pastsimple == a[b][2] ? 'past participle' : 'retry'
pastpart = gets.chomp
puts pastpart == a[b][3] ? 'compliments' : 'oh, no'

我得到以下输出:

$arr = array("define", "two dimensional", "three dimensional");
$var = array("variables", "constant");
$allArray = $var + $arr;
print_r($allArray);

但我期待着这个:

Array
(
    [0] => variables
    [1] => constant
    [2] => three dimensional
)

2 个答案:

答案 0 :(得分:3)

数组联合运算符在手册中定义如下:

  

+运算符返回附加到左侧数组的右侧数组;对于存在于两个数组中的键,将使用左侧数组中的元素,并且将忽略右侧数组中的匹配元素。

来源:http://php.net/manual/en/language.operators.array.php

因此,对于基本的数字索引数组,只会使用右侧操作数(问题中为$arr)的“额外”值。这就是为什么你只看到添加了三维的原因。

如果您想保留所有值,则应该使用array_merge代替。对于数字索引数组,它将保留(并重新索引)所有值。

请参阅https://eval.in/893020以了解差异。

答案 1 :(得分:0)

所有关于键都考虑以下代码

echo str_pad('>===== Union', 60, '=')."\n";

$arr = array("define", "two dimensional", "three dimensional");
$var = array("variables", "constant");
$allArray = $var + $arr;
var_dump($allArray);


echo str_pad('>===== Union With Assoc', 60, '=')."\n";


$arr = array('one'=>"define", 'two'=>"two dimensional", 'three' => "three dimensional");
$var = array('four' => "variables", 'five' => "constant");
$allArray = $var + $arr;
var_dump($allArray);

echo str_pad('>===== Replace ', 60, '=')."\n";

$arr = array("define", "two dimensional", "three dimensional");
$var = array("variables", "constant");
$allArray = array_replace($var,$arr);
var_dump($allArray);

echo str_pad('>===== Replace With Assoc', 60, '=')."\n";

$arr = array('one'=>"define", 'two'=>"two dimensional", 'three' => "three dimensional");
$var = array('four' => "variables", 'five' => "constant");
$allArray = array_replace($var,$arr);
var_dump($allArray);

echo str_pad('>===== Merge ', 60, '=')."\n";

$arr = array("define", "two dimensional", "three dimensional");
$var = array("variables", "constant");
$allArray = array_merge($var,$arr);
var_dump($allArray);

输出:

>===== Add==================================================
array(3) {
  [0]=>
  string(9) "variables"
  [1]=>
  string(8) "constant"
  [2]=>
  string(17) "three dimensional"
}
>===== Add With Assoc=======================================
array(5) {
  ["four"]=>
  string(9) "variables"
  ["five"]=>
  string(8) "constant"
  ["one"]=>
  string(6) "define"
  ["two"]=>
  string(15) "two dimensional"
  ["three"]=>
  string(17) "three dimensional"
}
>===== Replace =============================================
array(3) {
  [0]=>
  string(6) "define"
  [1]=>
  string(15) "two dimensional"
  [2]=>
  string(17) "three dimensional"
}
>===== Replace With Assoc===================================
array(5) {
  ["four"]=>
  string(9) "variables"
  ["five"]=>
  string(8) "constant"
  ["one"]=>
  string(6) "define"
  ["two"]=>
  string(15) "two dimensional"
  ["three"]=>
  string(17) "three dimensional"
}
>===== Merge ===============================================
array(5) {
  [0]=>
  string(9) "variables"
  [1]=>
  string(8) "constant"
  [2]=>
  string(6) "define"
  [3]=>
  string(15) "two dimensional"
  [4]=>
  string(17) "three dimensional"
}

你可以在这里看到

http://sandbox.onlinephpfunctions.com/code/c0a08a0a31cf4c91ecba835ef86bf483751acda0

正如您所见,+使用replace()而不是merge()

这是预期的行为:

http://php.net/manual/en/language.operators.array.php

  

+运算符返回附加到左侧的右侧数组   阵列;对于存在于两个数组中的键,来自的数组   将使用左手数组,以及来自的匹配元素   右手阵列将被忽略。