合并Json Formate中数组的重复值

时间:2017-09-18 14:57:03

标签: php mysql json

我从位置MySql表中获取数据,结构如下所示 在我的PHP代码中

enter image description here

我需要在响应中发送Json输出,如下所示(表数据可能与json数据不同,但格式相同)

 http://jsoneditoronline.org/?id=7c5600712df6f9ec1f8fbb8a13aba3de 

尝试在代码中执行以下操作以转换我从表中获取的数组但是无法以正确的格式获取它

  $sql = "SELECT * FROM mobile_user_regions";
    $stmt = $this->db->prepare($sql);
    $stmt->execute();
    $resCArray = $stmt->fetchAll(PDO::FETCH_ASSOC);

$ress = array();

foreach ($resCArray as $key => $value) {

    $ress['regions'][] = array(array(
            'name' => $value['region'],
            'location' => array(
                array(
                    'name' => $value['location'],
                    'store' => array(
                        'store_details' => $value['store_details'],
                        'store_phone' => $value['store_phone'],
                        'store_email' => $value['store_email'],
                        'store_latitude' => $value['store_latitude'],
                        'store_longitude' => $value['store_longitude']
                    )
                )
            )
        )
    );


 Output: that i am getting is 
 **http://jsoneditoronline.org/?id=4d4a75177350e254ceee7238af13f2f7**

1 个答案:

答案 0 :(得分:0)

$regionArray = $xyzObject->getRegion();
if (!empty($regionArray) && isset($regionArray)) {
    $i = 0;
    $locationData = array();
    foreach ($regionArray as $key => $value) {
        $locationData['regions'][$i]['name'] = $value['region'];

        $locationArray = $xyzObject->getLocation($value['region']);
        $locationData['regions'][$i]['locations'] = $locationArray;
        $i++;
    }
    $j = 0;
    foreach ($locationData['regions'] as $key => $regions) {
        $k = 0;
        foreach ($regions['locations'] as $key1 => $locations) {

            $storeArray = $xyzObject->getStore($locations['name']);
            $locationData['regions'][$j]['locations'][$k]['stores'] = $storeArray;
            $k++;
        }
        $j++;
    }
    echo json_encode($locationData);