Php 从数据库生成JSON布尔值

Php 从数据库生成JSON布尔值,php,mysql,json,yii2,Php,Mysql,Json,Yii2,我需要使用以下命令生成JSON字符串: 从这些表格中: 品牌: | id | name | description | icon | url | |----|--------|-------------|------|-----| | 1 | name 1 | description | icon | url | | 2 | name 2 | description | icon | url | | 3 | name 3 | description | icon | url | |

我需要使用以下命令生成JSON字符串:

从这些表格中:

品牌:

| 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 |
类别:

| 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 |
对象:

| 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);
}
你能帮我吗


关于

建立品牌后,在类别上循环,搜索现有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;
});
(注意在($i=1,$j=0;$i<$total;$i++)的
循环结束后放置此项)

这应该得到品牌中每个品牌的分类,正如你所希望的那样

如果需要将类别设置为文字“真”和“假”,请调整上面的这一行:

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

不要添加链接,直到它是如此的链接,而是把确切的内容放在这里。如果明天坏了怎么办?你的问题将毫无意义。你的意思是,
category 1
是真的,因为有一个
品牌id 1
,而
类别3
为false,因为没有
品牌id 3
?在“类别”表中没有“已启用”字段。如果对象位于一个或多个类别上,则每个类别都需要为true,否则为false,并且您希望此类别子组位于所有品牌上,即使对每一个人来说都是一样的,我希望结果会很好。还有一个数组搜索可以减少foreach,但是,我自己也喜欢foreach:)现在我想让它工作,lol,我稍后会尝试优化它,再次感谢