In_array php和string

时间:2014-06-06 13:12:32

标签: php

我有以下php数组会话:

$_SESSION["test"] = array(1,2,3,4,5,6,7);
array_walk_recursive($_SESSION["test"],"prepareArray");

function prepareArray(&$item,$key){
    $item = "'" . $item . "'";
}

debugging $_SESSION["test"]输出以下内容:

var_dump: 
array(7) { [0]=> string(3) "'1'" [1]=> string(3) "'2'" [2]=> string(3) "'3'" [3]=> string(3) "'4'" [4]=> string(3) "'5'" [5]=> string(3) "'6'" [6]=> string(3) "'7'" } 

print_r: 
Array ( [0] => '1' [1] => '2' [2] => '3' [3] => '4' [4] => '5' [5] => '6' [6] => '7' ) 

echo: 
Array

echo in_array('3',$_SESSION["test"],true);  //strict set to true (echoes nothing)
echo in_array('3',$_SESSION["test"],false); //strict set to false (echoes nothing)

我无法弄清楚为什么会发生这种情况。对这一个有任何想法吗?

1 个答案:

答案 0 :(得分:3)

prepareArray功能正在改变您的价值观,您应该搜索"' 3'"而不是' 3',试试这个:

echo in_array("'3'",$_SESSION["test"],true);
echo in_array("'3'",$_SESSION["test"],false);