定义索引和if语句

时间:2015-02-04 12:32:33

标签: php mysql json amazon-web-services

我正在尝试将一组JSON文件映射到一个mysql数据库,这里是一个json示例:

{

    "fileVersion": "1.0",
    "configurationItems": 

[

{

    "configurationItemVersion": "1.0",
    "configurationItemCaptureTime": "2014-12-12T17:14:09.005Z",
    "configurationStateId": 32,
    "relatedEvents": 

[

    "42d9f9c9-121c-4053-9ea8-451e98eeaf83"

],
"awsAccountId": "",
"configurationItemStatus": "OK",
"resourceId": "",
"ARN": "arn:aws:ec2:us-east-1:037734006438:vpc/vpc-ab8390c9",
"awsRegion": "us-east-1",
"availabilityZone": "Multiple Availability Zones",
"configurationStateMd5Hash": "",
"resourceType": "AWS::EC2::VPC",
"tags": { },
"relationships":
[

{

使用以下php我得到一个未定义的索引AWS :: EC2 :: VPC错误:

$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=$configurationItems;


                if($cfi=(["resourceType"]["AWS::EC2::VPC"]))
                {
                        $configurationItemVersion=$cfi["configurationItemVersion"]; 
                          echo "<br />","configuration_Item_Version:",$configurationItemVersion,"<br />"; 
                        $configurationItemCaptureTime=$cfi["configurationItemCaptureTime"]; 
                          echo "configurationItemCaptureTime:",$configurationItemCaptureTime,"<br />"; 
                        $configurationStateId=$cfi["configurationStateId"]; 
                          echo "configurationStateId:",$configurationStateId,"<br />"; 
                        $awsAccountId=$cfi["awsAccountId"]; 
                          echo "awsAccountId:",$awsAccountId,"<br />"; 
                        $configurationItemStatus=$cfi["configurationItemStatus"]; 
                          echo "configurationItemStatus:",$configurationItemStatus,"<br />"; 
                        $resourceId=$cfi["resourceId"]; 
                          echo "resourceId:",$resourceId,"<br />"; 
                        $configurationStateMd5Hash=$cfi["configurationStateMd5Hash"]; 
                          echo "configurationStateMd5Hash:",$configurationStateMd5Hash,"<br />"; 
                        $resourceType=$cfi["resourceType"]; 
                          echo "resourceType:",$resourceType,"<br />";   
                        $ARN= isset($cfi["ARN"]) ? $cfi["ARN"] : ''; 
                          echo "ARN:",$ARN,"<br />";
                        $awsRegion= isset($cfi["awsRegion"]) ? $cfi["awsRegion"] : ''; 
                          echo "awsRegion:",$awsRegion,"<br />";  
                        $availabilityZone= isset($cfi["availabilityZone"]) ? $cfi["availabilityZone"] : ''; 
                          echo "availabilityZone:",$availabilityZone,"<br />";
                        $resourceCreationTime= isset($cfi["resourceCreationTime"]) ? $cfi["resourceCreationTime"] : ''; 
                          echo "resourceCreationTime:",$resourceCreationTime,"<br />";










                       $result = mysqli_query($con, "INSERT INTO     cfi_vpc(configuration_item_version,configuration_item_capture_time,configuration_state_id, aws_account_id, configuration_item_status, resource_id, arn, aws_region, availability_zone,configuration_state_md5_hash, resource_type, resource_creation_time)

                       VALUES('$configurationItemVersion','$configurationItemCaptureTime','$configurationStateId','$awsAccountId','$configurationItemStatus','$resourceId','$ARN','$awsRegion','$availabilityZone','$configurationStateMd5Hash','$resourceType','$resourceCreationTime' )")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 configuration items volume"; 

                        // echoing JSON response 
                        echo json_encode($response); 
                    }
                    else
                    { 
                        echo mysql_error();
                        // failed to insert row 
                        $response["code"] = 2; 
                        $response["message"] = "Oops! An error occurred."; 

                        // echoing JSON response 
                        echo json_encode($response); 
                    } 
                } 
            } 
        }
    }
}
?> 

我想象的方式是,如果资源类型等于vpc或其他类型(如实例或安全组),则将相应的数据放入正确的表中,谢谢

1 个答案:

答案 0 :(得分:1)

if($cfi=(["resourceType"]["AWS::EC2::VPC"]))

????毫无意义

// check whether a the literal `AWS::EC2::VPC` equals
// the value of the element resourceType in $cfi
if( "AWS::EC2::VPC"===$cfi["resourceType"] )