dongpan1308 2018-01-31 20:44
浏览 21
已采纳

即将发出警告

My following code works (it returns the value of text in 2d array). Now, this is just a proof of concept for something more I want to do (so that's why the logic is stupid)

function searchArrayFor($array, $id, $searchFor) {
    $index = 0;
    foreach ($array as $key => $value)
    {
        return $value[1];
    }
}

So I noticed in my log file, that I am constantly getting "PHP Warning: Invalid argument supplied for foreach()". Everything looks right, and returns the value I want, any ideas on why I keep getting this error? I really don't want to be spamming the log file with every page load.

using PHP version 5.3.28 (I know, really old version).

Thanks

  • 写回答

1条回答 默认 最新

  • dongsha7215 2018-01-31 20:51
    关注

    Try this I think you might have variables that are not array

    function searchArrayFor($array, $id, $searchFor) {
        $index = 0;
        if(is_array($array)) {
            foreach ($array as $key => $value)
            {
                return $value[1];
            }
        }
        // Maybe add ELSE here and do a die($array); to see which value is not array
    }
    

    I believe this will take care of your array warning, let me know if that works.

    Cheers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度