多维数组将相同的键和值组合到已定义的数组元素中

时间:2017-09-01 11:27:47

标签: php mysql arrays multidimensional-array

我是php的新手,获取存储json的mysql结果,但它不是我想要的gettng coorect格式。

检查以下代码

$sql = "select * from en_providers where providerEmailAddress='" . $email . "' and providerPW='" . $password . "'";
$result = mysqli_query($con, $sql) or die("Error in Selecting " . mysqli_error($connection));
if (mysqli_num_rows($result) > 0) {
    $resultArray = array();
    while ($row = mysqli_fetch_assoc($result)) {
        $providerID               = $row['providerID'];
        $resultArray['providers'] = $row;
        $resultArray['providers']['providerIDActivities'] = unserialize($row['providerIDActivities']);
        $resultArray['providers']['providerIDBodies'] = unserialize($row['providerIDBodies']);
        $resultArray['providers']['providerIDOthers'] = unserialize($row['providerIDOthers']);

        $sql1 = "select * from en_venues where providerID = $providerID ";
        $result1 = mysqli_query($con, $sql1) or die("Error in Selecting " . mysqli_error($connection));
        $i = $j = $l = $x = $m = 0;
        while ($row1[] = mysqli_fetch_assoc($result1)) {
            //$resultArray['venues'][]['venueIDFacilities'] = unserialize($row1[$j++]['venueIDFacilities']);
            $venueID                                          = $row1[$j++]['venueID'];
            $k                                                = 0;
            $venueFacilities                                  = unserialize($row1[$i++]['venueIDFacilities']);
            $resultArray[$x++]['venues']['venueIDFacilities'] = $venueFacilities;

            //$resultArray['venues'][$x++]['venueID'] = $venueID;
            $resultArray['venues'] = $row1;
            //echo json_encode($resultArray);



        echo json_encode($resultArray);
    }
}

输出是:

{
    0: {
        "venues": {
            "venueIDFacilities": [
                "1",
                "2",
                "3"
            ],
        }
    },
    1: {
        "venues": {
            "venueIDFacilities": [
                "4",
                "7"
            ],
        }
    }
},
"providers": {
    "providerIDActivities": [
        "218",
        "219"
    ],
    "providerIDSports": "a:1:{i:0;i:82;}",
    "providerIDBodies": [
        "112"
    ],
},
venues": {
    0: {
        "venueID": "9",
        "providerID": "2"
    },
    1: {
        "venueID": "238",
        "providerID": "2",
        "venueActive": "yes"
    }
}

但是我需要那些VenueFailities在各自的场地,但结果却在外面。如何将这些值附加到场地?

我想用不同的方式尝试一天,但是格式不正确。

我想要的输出:

"providers": {
    "providerIDActivities": [
        "218",
        "219"
    ],
    "providerIDSports": "a:1:{i:0;i:82;}",
    "providerIDBodies": [
        "112"
    ],
},
venues": {
    0: {
        "venueID": "9",
        "providerID": "2",
        "venueIDFacilities": [
            "4",
            "7"
        ]
    },
    1: {
        "venueID": "238",
        "providerID": "2",
        venueIDFacilities": [
            "4",
            "7"
        ]
    }
}

1 个答案:

答案 0 :(得分:1)

这一行

$resultArray[$x++]['venues']['venueIDFacilities'] = $venueFacilities;

应该是

$resultArray['venues'][$x++]['venueIDFacilities'] = $venueFacilities;