なぜこれが本当なのですか?パーティバウンスとスライド13 x18はifステートメントに含まれていませんか?
print_r($btypes);
//Array ( [0] => 11x13 Red and Yellow 2 [1] => Party Bounce and Slide 13 x 18 )
if(in_array("12x15 Purple and Yellow 2" || "11x13 Red and Yellow 1" || "12x15 Yellow and Purple old" || "11x13 Red and Yellow 2" || "12x15 Purple and Yellow 1" ,$btypes) &&
in_array("Bootcamp Obstacle Course" || "Terminator Torment Obstacle Course" || "Lizard Obstacle Course" || "Bugs Obstacle Course" || "Nemo Obstacle Course",$btypes))
{
return "Standard Bouncy Castle and an Obstacle Course";
}
ご協力いただきありがとうございます
"12x15 Purple and Yellow 2" || "11x13 Red and Yellow 1"
は論理式です。ブールコンテキストでは、""
を除く任意の文字列 および"0"
TRUE
として評価されます 。上記の式はTRUE || TRUE
と同じ値を持ちます その値はTRUE
です 。デフォルトでは、
in_array()
==
を使用します 最初の引数を2番目の引数として渡された配列の各要素と比較します。in_array()
に渡す最初の引数 はTRUE
です そしてそれは==
です""
ではない任意の文字列に または"0"
。$btypes
のすべての文字列==
ですTRUE
へ およびin_array()
TRUE
を返します 。PHPが異なるタイプの値を比較する方法、および
==
を使用して比較した場合に異なる値がどのように動作するかについて読む および===
。あなたが達成したいものに応じて、
array_intersect()
を使用することをお勧めします および/またはarray_diff()
$btypes
にも存在する値を取得する 。$sizes = [ '12x15 Purple and Yellow 2', '11x13 Red and Yellow 1', '12x15 Yellow and Purple old', '11x13 Red and Yellow 2', '12x15 Purple and Yellow 1', ]; $courses = [ 'Bootcamp Obstacle Course', 'Terminator Torment Obstacle Course', 'Lizard Obstacle Course', 'Bugs Obstacle Course', 'Nemo Obstacle Course', ]; $btypes = [ '11x13 Red and Yellow 2', 'Party Bounce and Slide 13 x 18', ]; if (count(array_intersect($sizes, $btypes)) && count(array_intersect($courses, $btypes))) { // At least one size and at least one course are present in $btypes return "Standard Bouncy Castle and an Obstacle Course"; }
コードを再フォーマットすると、ここで行われていることを簡単に追跡できます。
<?php function is_bouncy_obstacle() { $btypes = [ '11x13 Red and Yellow 2', 'Party Bounce and Slide 13 x 18' ]; if( in_array( "12x15 Purple and Yellow 2" || "11x13 Red and Yellow 1" || "12x15 Yellow and Purple old" || "11x13 Red and Yellow 2" || "12x15 Purple and Yellow 1" , $btypes ) && in_array( "Bootcamp Obstacle Course" || "Terminator Torment Obstacle Course" || "Lizard Obstacle Course" || "Bugs Obstacle Course" || "Nemo Obstacle Course" , $btypes ) ) { return "Standard Bouncy Castle and an Obstacle Course"; } } var_dump(is_bouncy_obstacle());
出力:
string(45) "Standard Bouncy Castle and an Obstacle Course"
したがって、trueではなく文字列を返します。ただし、比較で上記の文字列を使用する場合、真であると見なされます。
重要なのは
in_array
針と干し草の2つの引数を取ります。干し草の山で針を探しています。引数の順序は重要です。in_array
の使用例 :var_dump(in_array('apple', ['apple', 'orange', 'pear'])); var_dump(in_array('apple', ['banana', 'papaya', 'sultana']));
出力:
bool(true) bool(false)
コードに戻ります。
考慮してください:
var_dump('foo'); var_dump('foo' || 'bar'); var_dump(in_array('foo' || 'bar', ['baz'])); var_dump(in_array(true, ['qux']));
出力:
string(3) "foo" bool(true) bool(true) bool(true)
最後は少し奇妙に感じます!これは、
in_array
のデフォルトである緩やかな比較が原因です。 。条件を書き直す必要があります。
管理が簡単で、さらに重要なクイックソリューション。