从Db生成JSON布尔值

时间:2018-01-27 03:57:04

标签: php mysql json yii2

我需要使用this format

生成JSON字符串
{"content":{"brands":1},"brands":[{"id":"1","name":"brand 1","description":"description","icon":"icon","url":"example.com","categories":{"1":"true","2":"true","3":"false","4":"false","5":"false","6":"false"}},{"id":"2","name":"brand2","description":"description","icon":"icon","url":"example.com","categories":{"1":"true","2":"true","3":"false","4":"false","5":"false","6":"false"}}]}

从这些表中:

品牌:

| id | name   | description | icon | url |
|----|--------|-------------|------|-----|
|  1 | name 1 | description | icon | url |
|  2 | name 2 | description | icon | url |
|  3 | name 3 | description | icon | url |
|  4 | name 4 | description | icon | url |
|  5 | name 5 | description | icon | url |
|  6 | name 6 | description | icon | url |

类:

| id | name   | description | icon | url |
|----|--------|-------------|------|-----|
|  1 | name 1 | description | icon | url |
|  2 | name 2 | description | icon | url |
|  3 | name 3 | description | icon | url |
|  4 | name 4 | description | icon | url |
|  5 | name 5 | description | icon | url |
|  6 | name 6 | description | icon | url |

物体

| id | id_brand | id_category |name   | description | icon | url |
|----|----------|-------------|-------|-------------|------|-----|
|  1 |     1    |       1     |name 1 | description | icon | url |
|  2 |     1    |       2     |name 2 | description | icon | url |
|  3 |     2    |       1     |name 3 | description | icon | url |
|  4 |     2    |       2     |name 4 | description | icon | url |

这是我目前的相关代码

public function actionBrand($id = null) {
    if (empty($id)) {
        // Obtiene datos de la base
        $sql = "SELECT DISTINCT objects.id_brand AS id, brands.name AS name, brands.description AS description, brands.icon AS icon, brands.url AS url, objects.id_category, categories.name AS category " .
                "FROM objects " .
                "LEFT JOIN brands ON objects.id_brand = brands.id " .
                "LEFT JOIN categories ON objects.id_category = categories.id " .
                "ORDER BY objects.id_brand, objects.id_category ";
    } else {
        // Obtiene datos de la base
        $sql = "SELECT DISTINCT objects.id_brand AS id, brands.name AS name, brands.description AS description, brands.icon AS icon, brands.url AS url, objects.id_category, categories.name AS category " .
                "FROM objects " .
                "LEFT JOIN brands ON objects.id_brand = brands.id " .
                "LEFT JOIN categories ON objects.id_category = categories.id " .
                "WHERE brands.id = " . (int) $id . " " .
                "ORDER BY objects.id_brand, objects.id_category ";
    }

    $data = Yii::$app->db->createCommand($sql)
            ->queryAll();

    // Obtiene categorias
    $categories = Yii::$app->db->createCommand('SELECT id FROM categories ORDER BY id')
            ->queryAll();

    if (!empty($data)) {
        // Construye primer registro
        $brands[0]['id'] = $data[0]['id'];
        $brands[0]['name'] = $data[0]['name'];
        $brands[0]['description'] = $data[0]['description'];
        $brands[0]['icon'] = $data[0]['icon'];
        $brands[0]['url'] = $data[0]['url'];

        $total = count($data);
        for ($i = 1, $j = 0; $i < $total; $i++) {
            if ($brands[$j]['id'] == $data[$i]['id']) {
                continue;
            } else {
                $j++;

                $brands[$j]['id'] = $data[$i]['id'];
                $brands[$j]['name'] = $data[$i]['name'];
                $brands[$j]['description'] = $data[$i]['description'];
                $brands[$j]['icon'] = $data[$i]['icon'];
                $brands[$j]['url'] = $data[$i]['url'];
            }
        }
    } else {
        $brands = array();
    }

    // Construye y envia JSON
    $json['content']['brands'] = count($brands);
    $json['brands'] = $brands;
    echo json_encode($json);
}

它会生成我需要的JSON的first part,但我会在类别部分停留,我需要从基础中选择数据并将其转换为每个品牌的id:(true)(false)< / p>

{"content":{"brands":1},"brands":[{"id":"1","name":"brand 1","description":"description","icon":"icon","url":"example.com"},{"id":"2","name":"brand2","description":"description","icon":"icon","url":"example.com"}]}

你能帮助我吗?

此致

1 个答案:

答案 0 :(得分:3)

完成品牌建设后,循环使用类别,搜索品牌以查找现有ID匹配。如果匹配,则为类别调整true false值。然后,当类别完成后,将它们附加到最终循环中的每个品牌。

$cleancats = [];
foreach ($categories as $cat) {
    $result = false;
    foreach($brands as $brand) {
        if ($cat['id'] == $brand['id']) {
            $result = true; break;
        }
    }
    $cleancats[ $cat['id'] ] = $result;
}
array_walk ($brands,function(&$brand) use ($cleancats) {
    $brand['categories'] = $cleancats;
});

(请注意,在for ($i = 1, $j = 0; $i < $total; $i++)循环结束后)

这应该按照您的意愿获得品牌中每个品牌的类别。

如果您需要将类别设为LITERAL“true”和“false”,请调整以上一行:

    $cleancats[ $cat['id'] ] = ($result?'true':'false');