Sois
Sois

Reputation: 33

Get a value from array via a function in PHP

I have a problem in my PHP code. 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

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

    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 :

What am I doing wrong?

Upvotes: 0

Views: 85

Answers (4)

Ashish-Joshi
Ashish-Joshi

Reputation: 231

You need to do some changes in isnull_post() function as your $db_res array scope is not global. 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.

Upvotes: 1

Keyur Mistry
Keyur Mistry

Reputation: 926

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.

Upvotes: 0

GreyRoofPigeon
GreyRoofPigeon

Reputation: 18123

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:

<?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.' , ');
    }
?>

Upvotes: 0

Rahul Kaushik
Rahul Kaushik

Reputation: 1464

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.' , ';
}

Upvotes: 0

Related Questions