[英]in_array not working as expected
So i have a very simple snippet. 所以我有一个非常简单的代码段。 I studied the
in_array
command and found it would be what i need. 我研究了
in_array
命令,发现这是我需要的。
However its not working? 但是它不起作用?
I have tried several scenarios: 我尝试了几种方案:
$this->item->tour_days
is an array containing 1,2,3 $this->item->tour_days
是一个包含1,2,3的数组
test 1 测试1
$days = $this->item->tour_days;
$array = array($days);
if (in_array(2,$array,TRUE)) {
echo 'Tuesday';
}
test 2 测试2
$days = $this->item->tour_days;
$array = array($days);
if (in_array(2,$array)) {
echo 'Tuesday';
}
test 3 测试3
$days = $this->item->tour_days;
$array = array($days);
if (in_array('2',$array)) {
echo 'Tuesday';
}
I have tried to echo Tuesday where Tuesday = 2 from my csv but no luck. 我试图从我的csv中回显星期二,其中星期二= 2,但是没有运气。
Thanks in advance for nay help here jonny 在此先感谢您的反对。
要将包含逗号分隔列表的字符串转换为数组,请使用explode
:
$array = explode(',', $days);
尝试这个:
$array = explode(",",$days);
$this->item->tour_days
is an array containing 1,2,3 $this->item->tour_days
是一个包含1,2,3的数组
Then why do you want to make another array out of it? 那为什么还要用它制作另一个数组呢?
remove the $array = array($days);
删除
$array = array($days);
converstion. 对话。
Instead say $array = $days;
而是说
$array = $days;
This should work for you: 这应该为您工作:
If $days
is a string then use this: 如果
$days
是一个字符串,请使用以下命令:
$days = "1,2,3"; //$this->item->tour_days;
$array = explode(',', $days);
if(in_array(2, $array))
echo "Tuesday";
?>
If $days
is already an array use this: 如果
$days
已经是一个数组,请使用以下命令:
<?php
$days = array(1,2,3); //$this->item->tour_days;
if(in_array(2, $days))
echo "Tuesday";
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.