php 多次递归传值出错,PHP-为什么此递归in_array函数错误地返回true?

这个问题已经在这里有了答案:            >            PHP in_array() / array_search() odd behaviour                                    2个

我正在尝试调试编写的脚本,并且有一个问题归结为检查(多维)资产数组中是否存在标识符.我正在使用一个in_array函数,该函数以递归方式搜索从this question获得的结果.

这是函数:

function in_array_r($needle, $haystack, $strict = false) {

foreach ($haystack as $item) {

if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {

return true;

}

}

return false;

}

我正在使用此数据:

针头:“ B51MM36”

干草堆:(针对未美化的数组的道歉-找不到从var_export美化的方法)

$sedols = array ( 0 => array ( 'ipsid' => '72', 'buyList' => '1', 'sedol' => 'B8LFDR7', 'isin' => 'LU0827876409', 'currency' => NULL, 'hedged' => '0', 'acc' => '0', 'inst' => '0', 'description' => 'BlackRock European Long Only', 'nonUKsitus' => '0', 'reportingStatus' => '0', 'matchScore' => 0, ), 1 => array ( 'ipsid' => '72', 'buyList' => '1', 'sedol' => 'LU0827876151', 'isin' => 'LU0827876151', 'currency' => 'USD', 'hedged' => '1', 'acc' => '1', 'inst' => '0', 'description' => 'Blackrock European Long Only', 'nonUKsitus' => '0', 'reportingStatus' => '0', 'matchScore' => 0, ), 2 => array ( 'ipsid' => '72', 'buyList' => '1', 'sedol' => 'LU0406496546 ', 'isin' => 'LU0406496546 ', 'currency' => 'EUR', 'hedged' => '1', 'acc' => '1', 'inst' => '0', 'description' => 'Blackrock European Long Only', 'nonUKsitus' => '0', 'reportingStatus' => '0', 'matchScore' => 0, ), 3 => array ( 'ipsid' => '72', 'buyList' => '1', 'sedol' => 'LU0827876409', 'isin' => 'LU0827876409', 'currency' => 'GBP', 'hedged' => '1', 'acc' => '0', 'inst' => '0', 'description' => 'Blackrock European Long Only', 'nonUKsitus' => '0', 'reportingStatus' => '1', 'matchScore' => 1, ), );

当我运行var_dump(in_array_r(‘B51MM36’,$sedols));它输出bool(true).我很困惑,因为字符串“ B51MM36”没有出现在干草堆数组中的任何位置.任何人都可以确定这里发生了什么吗?

解决方法:

原因是

var_dump('B51MM36' == 0);

是真的,不知道为什么(也许将字符串转换为整数),但是这项工作

var_dump(in_array_r('B51MM36', $sedols, true));

尝试删除严格的选项来源:https://www.icode9.com/content-1-536601.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值