خرید بک لینک

Vote count: 0

Sometimes we receive input data of varying structure, for example response from online API may include some information but other not, some details are stored in complex nested arrays etc. I like to parse this data before usage, this way I don't have to use isset() over and over later on, ex.:

$input; // source
$correct_data = arra(); // verified data
$correct_data["option-1"] = (isset($input["option-1"]) ? $input["option-1"] : "");
$correct_data["option-2"] = (isset($input["option-2"]) ? $input["option-2"] : "");

Now I can use:

my_function($correct_data["option-1"]);
my_function2($correct_data["option-2"]);

and I know that there won't be any waings for uninitialized variables or unknown array keys. But problem occurs for nested data e.g.

$input = array(
    "settings-main" => array(
        "option-1" => "val-1",
        "option-2" => "val-2",
        "sub-settings" => array(
            "my-option" => "some val",
            "my-option-2" => "some val2",
        ),
    ),
    "other-settings" => array(
        "other" => array(
            "option-1" => "a",
            "option-2" => "b",
        ),
    ),
);

It's difficult to check this on start, later I have to use something like this:

if(isset($input["settings-main"]))
{
    if(isset($input["settings-main"]["option-1"]))
        $input["settings-main"]["option-1"]; //do something
    if(isset($input["settings-main"]["sub-settings"]))
    {
        if(isset($input["settings-main"]["sub-settings"]["my-option-2"]))
            $input["settings-main"]["sub-settings"]["my-option-2"]; //do something
    }
}

do you have any suggestions how to handle such situations without using multiple isset() instructions ?

asked 53 secs ago

برچسب: نویسنده: استخدام کار تاريخ: شنبه 15 خرداد 1395 ساعت: 0:25

صفحه بندی