简体   繁体   English

通过PHP中的函数从数组获取值

[英]Get a value from array via a function in PHP

I have a problem in my PHP code. 我的PHP代码有问题。 I got an array like this as example ( this array is created dynamic with data from a database). 我得到了一个像这样的数组作为示例(该数组是使用数据库中的数据动态创建的)。

$db_res =array('debiteur_id'=>'1020','user_id'=>'495','b2b_or_b2c'=>'B2C');

When I show the values with 当我用显示值时

foreach ($db_res as $key => $value )
{
   echo $key.' - '.$value.' , ';
}

It is displaying these values what is OK 它显示这些值没关系

debiteur_id - 1020 , user_id - 495 , b2b_or_b2c - B2C debiteur_id-1020,user_id-495,b2b_or_b2c-B2C

Now I have a function to test if some data in the array is set or not and to return some values from this $db_res array of $_POST array 现在,我有一个函数可以测试是否设置了数组中的某些数据,并从$ _POST数组的$ db_res数组返回一些值

    function isnull_post($naam)
    {
        if (isset($db_res[$naam]))
            return($db_res[$naam]);
        else
        {
            if (isset($_POST[$naam]))
                return($_POST[$naam]);
            else
                return('');
        }
    }

When I use the following code to show my array 当我使用以下代码显示我的数组时

    foreach ($db_res as $key => $value )
    {
        echo $key.' - '.$value.' , ';
        $val = isnull_post($key);
        echo ('isnull : '.$val.' , ');
    }

This is my output 这是我的输出

debiteur_id - 1020 , isnull : , user_id - 495 , isnull : , b2b_or_b2c - B2C , isnull : debiteur_id-1020,isnull:,user_id-495,isnull:,b2b_or_b2c-B2C,isnull:

What am I doing wrong? 我究竟做错了什么?

You need to do some changes in isnull_post() function as your $db_res array scope is not global. 由于$ db_res数组作用域不是全局的,因此需要在isull_post()函数中进行一些更改。 So make it global in the function where you want to use it as a global variable. 因此,在要用作全局变量的函数中将其设为全局。

function isnull_post($naam)
    {   
            global $db_res;

            if (isset($db_res[$naam]))
                return($db_res[$naam]);
            else
            {
                if (isset($_POST[$naam]))
                    return($_POST[$naam]);
                else
                    return('');
            }
    }

You can also do it by passing the array to the function as : 您也可以通过以下方式将数组传递给函数:

 $db_res =array('debiteur_id'=>'1020','user_id'=>'495','b2b_or_b2c'=>'B2C');

    function isnull_post($naam,$db_res)
        {   
            if (isset($db_res[$naam]))
            return($db_res[$naam]);
            else
            {
                if (isset($_POST[$naam]))
                    return($_POST[$naam]);
                else
                    return('');
            }
        }

        foreach ($db_res as $key => $value )
        {   
            $val = isnull_post($key,$db_res);
            echo $key.' - '.$val.' , ';
        }

if your requirement for using this array is only for this function,then you can pass it to the function but if other functions requires this array then you have to define the array global to that function's body scope. 如果您只需要为此函数使用此数组,则可以将其传递给该函数,但是如果其他函数需要此数组,则必须定义该函数的主体范围的全局数组。

Use Below code: 使用以下代码:

$db_res =array('debiteur_id'=>'1020','user_id'=>'495','b2b_or_b2c'=>'B2C');
function isnull_post($naam,$db_res)
{   
    if (isset($db_res[$naam]))
    return($db_res[$naam]);
    else
    {
        if (isset($_POST[$naam]))
            return($_POST[$naam]);
        else
            return('');
    }
}

foreach ($db_res as $key => $value )
{   
    $val = isnull_post($key,$db_res);
    echo $key.' - '.$val.' , ';
}

As already is commented, the variable $db_res isn't accessible in your function, you either make it a global (which I don't prefer), or you pass it on, like this: 如前所述,变量$db_res在您的函数中不可访问,您可以将其$db_res变量(我不希望使用此变量),或者将其传递,如下所示:

<?php
    function isnull_post($naam, $db_res)
    {
        if (isset($db_res[$naam]))
            return($db_res[$naam]);
        else
        {
            if (isset($_POST[$naam]))
                return($_POST[$naam]);
            else
                return('');
        }
    }


    foreach ($db_res as $key => $value )
    {
        echo $key.' - '.$value.' , ';
        $val = isnull_post($key, $db_res);
        echo ('isnull : '.$val.' , ');
    }
?>

You can write any of the way, 您可以用任何方式写

        $db_res =array('debiteur_id'=>'1020','user_id'=>'495','b2b_or_b2c'=>'B2C');
        function isnull_post($naam)
        {   
            global $db_res;
            if (isset($db_res[$naam]))
            return($db_res[$naam]);
            else
            {
                if (isset($_POST[$naam]))
                    return($_POST[$naam]);
                else
                    return('');
            }
        }

        foreach ($db_res as $key => $value )
        {   
            $val = isnull_post($key);
            echo $key.' - '.$val.' , ';
        }

Or as the following Steps.. 或按以下步骤操作。

            $db_res =array('debiteur_id'=>'1020','user_id'=>'495','b2b_or_b2c'=>'B2C');
        function isnull_post($naam,$db_res)
        {   
            if (isset($db_res[$naam]))
            return($db_res[$naam]);
            else
            {
                if (isset($_POST[$naam]))
                    return($_POST[$naam]);
                else
                    return('');
            }
        }

        foreach ($db_res as $key => $value )
        {   
            $val = isnull_post($key,$db_res);
            echo $key.' - '.$val.' , ';
        }

But I prefer to use first one. 但是我更喜欢使用第一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM