doushenyi9104 2014-08-04 11:34
浏览 36

如何在函数中传递数组索引并在函数中检查此索引是否设置为php

I have an associative array in php name as

$formData =array(
    "first"=>"John",
    "second"=>"George",
    "thirld"=>"Harry",
    "fourth"=>"Tom"
);

and a function called isExist($v) as

function isExist($v) {
    if(isset($v)) {
        return $v;
    } 
    return ""; 
}

now i want that this function check either array have index value set or not i.e if I call the function as

echo isExist($formData['apple']);

then it must print an empty string. How to achieve this? Thanks in advanced.

  • 写回答

1条回答 默认 最新

  • du0204 2014-08-04 11:36
    关注

    There is no way you can achieve this with passing $formData['apple'] you need to pass the array and index separately

    function isExist($array,$index){
        if(isset($array[$index])){
        return $index;
       } 
      return ""; 
    }
    

    The error will be raised before it ever had the chance to get in the function because you already called it when you were passing it in as parameter. Use it like this instead

    isExist($formData,'apple');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM