isset方法不工作什么都不返回

时间:2015-01-27 13:13:03

标签: php

我正在尝试将一组JSON文件映射到数据库中,但我的isset检查在我的PHP中不起作用:

    <?php
$con = mysqli_connect("localhost", "root", "", "json_map");
$response = array();
$res = array();
$result = '';

foreach(glob('C:\xampp\htdocs\laravel\awsconfig\app\views\*.json') as $filename)
    {
    $json = file_get_contents($filename);
    if ($json != null)
        {
        $decoded = json_decode($json, true);

        // $decode= var_dump($decoded);
        // $ss=$decode["array"];
        // echo $decoded['number'];

        if (is_array($decoded["configurationItems"]))
            {
            foreach($decoded["configurationItems"] as $configurationItems)

            // for($i=0;$i>sizeof($decoded["configurationItems"]);$i++)

                {
                $cfi = isset($configurationItems["configuration"]) ? $configurationItems["configuration"] : '';
                if (isset($cfi["instanceId"]) && isset($cfi["imageId"]) && isset($cfi["privateDnsName"]) && isset($cfi["publicDnsName"]) && isset($cfi["keyName"]) && isset($cfi["stateTransitionReason"]) && isset($cfi["amiLaunchIndex"]) && isset($cfi["instanceType"]) && isset($cfi["launchTime"]) && isset($cfi["kernelId"]) && isset($cfi["subnetId"]) && isset($cfi["vpcId"]) && isset($cfi["privateIpAddress"]) && isset($cfi["architecture"]) && isset($cfi["rootDeviceType"]) && isset($cfi["rootDeviceName"]) && isset($cfi["virtualizationType"]) && isset($cfi["clientToken"]) && isset($cfi["sourceDestCheck"]) && isset($cfi["hypervisor"]) && isset($cfi["ebsOptimized"]))
                    {
                    $instanceId = $configurationItems["configuration"]["instanceId"];
                    echo "instanceId:", $instanceId, "<br />";
                    $imageId = $configurationItems["configuration"]["imageId"];
                    echo "imageId:", $imageId, "<br />";
                    $privateDnsName = $configurationItems["configuration"]["privateDnsName"];
                    echo "privateDnsName:", $privateDnsName, "<br />";
                    $publicDnsName = $configurationItems["configuration"]["publicDnsName"];
                    echo "publicDnsName:", $publicDnsName, "<br />";
                    $keyName = $configurationItems["configuration"]["keyName"];
                    echo "keyName:", $keyName, "<br />";
                    $stateTransitionReason = $configurationItems["configuration"]["stateTransitionReason"];
                    echo "stateTransitionReason:", $stateTransitionReason, "<br />";
                    $amiLaunchIndex = $configurationItems["configuration"]["amiLaunchIndex"];
                    echo "amiLaunchIndex:", $amiLaunchIndex, "<br />";
                    $instanceType = $configurationItems["configuration"]["instanceType"];
                    echo "instanceType:", $instanceType, "<br />";
                    $launchTime = $configurationItems["configuration"]["launchTime"];
                    echo "launchTime:", $launchTime, "<br />";
                    $kernelId = $configurationItems["configuration"]["kernelId"];
                    echo "kernelId:", $kernelId, "<br />";
                    $subnetId = $configurationItems["configuration"]["subnetId"];
                    echo "subnetId:", $subnetId, "<br />";
                    $vpcId = $configurationItems["configuration"]["vpcId"];
                    echo "vpcId:", $vpcId, "<br />";
                    $privateIpAddress = $configurationItems["configuration"]["privateIpAddress"];
                    echo "privateIpAddress:", $privateIpAddress, "<br />";
                    $architecture = $configurationItems["configuration"]["architecture"];
                    echo "architecture:", $architecture, "<br />";
                    $rootDeviceType = $configurationItems["configuration"]["rootDeviceType"];
                    echo "rootDeviceType:", $rootDeviceType, "<br />";
                    $rootDeviceName = $configurationItems["configuration"]["rootDeviceName"];
                    echo "rootDeviceName:", $rootDeviceName, "<br />";
                    $virtualizationType = $configurationItems["configuration"]["virtualizationType"];
                    echo "virtualizationType:", $virtualizationType, "<br />";
                    $clientToken = $configurationItems["configuration"]["clientToken"];
                    echo "clientToken:", $clientToken, "<br />";
                    $sourceDestCheck = $configurationItems["configuration"]["sourceDestCheck"];
                    echo "sourceDestCheck:", $sourceDestCheck, "<br />";
                    $hypervisor = $configurationItems["configuration"]["hypervisor"];
                    echo "hypervisor:", $hypervisor, "<br />";
                    $ebsOptimized = $configurationItems["configuration"]["ebsOptimized"];
                    echo "ebsOptimized:", $ebsOptimized, "<br />";
                    $result = mysqli_query($con, "INSERT INTO     configuration(instance_id, image_id, private_dns_name, public_dns_name, key_name, state_transition_reason, ami_launch_index, instance_type, launch_time, kernel_id, subnet_id, vpc_id, private_ip_address,architecture, root_device_type, root_device_name, virtualisation_type, client_token, source_dest_check, hypervisor, ebs_optimised)


VALUES('$instanceId','$imageId', '$privateDnsName' , '$publicDnsName', '$keyName', '$stateTransitionReason', '$amiLaunchIndex', '$instanceType', '$launchTime', '$kernelId', '$subnetId', '$vpcId', '$privateIpAddress', '$architecture', '$rootDeviceType', '$rootDeviceName', '$virtualizationType', '$clientToken', '$sourceDestCheck', '$hypervisor', '$ebsOptimized')") or die("Insert Failed " . ((is_object($con)) ? mysqli_error($con) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));;
                    } // check if row inserted or not
                if ($result)
                    {

                    // successfully inserted into database

                    $response["code"] = 1;
                    $response["message"] = "successfully updated config table ";

                    // echoing JSON response

                    echo json_encode($response);
                    }
                  else
                    {

                    // failed to insert row

                    $response["code"] = 2;
                    $response["message"] = "Oops! An error occurred.";

                    // echoing JSON response

                    echo json_encode($response);
                    }
                }
            }
        }
    }

?>

当我使用

$cfi = isset($configurationItems["configuration"]) ? $configurationItems["configuration"] : ''; 

我得到oops发生错误没有错误的解释

当我使用

$cfi = $configurationItems ["configuration"];

我得到上面加上未定义的索引配置(错误)

3 个答案:

答案 0 :(得分:0)

如果测试已设置为NULL的变量,

isset()将返回FALSE。

有关详细信息,请参阅link

答案 1 :(得分:0)

您可以使用所需的密钥构建验证数组,然后检查指定的数组是否包含所有这些密钥,而不必对条件所需的每个密钥使用isset

$required_keys = ['instanceId',
                  'imageId',
                  'privateDnsName',
                  'publicDnsName',
                  'keyName',
                  'stateTransitionReason',
                  'amiLaunchIndex',
                  'instanceType',
                  'launchTime',
                  'kernelId',
                  'subnetId',
                  'vpcId',
                  'privateIpAddress',
                  'architecture',
                  'rootDeviceType',
                  'rootDeviceName',
                  'virtualizationType',
                  'clientToken',
                  'sourceDestCheck',
                  'hypervisor',
                  'ebsOptimized'];

.........

if (isset($configurationItems['configuration'])) {
   if (count(array_intersect_ukey($configurationItems['configuration'], array_flip($required_keys), function($a, $b) {
                                                                                                       return -1 * ($a < $b) + ($a > $b);
                                                                                                    })) === count($required_keys)) {
       .....
   }
}

答案 2 :(得分:0)

支架位置错误

$cfi = isset($configurationItems["configuration"]) ? $configurationItems["configuration"] : '';`

有效处理