【问题标题】:What query would get me this result? JSON and PHP什么查询会给我这个结果? JSON 和 PHP
【发布时间】:2015-02-25 13:00:58
【问题描述】:
{
    "cars": {
        "toyota": [
            {"model":"*****", "doors":"*","color":"*****"},
            {"model":"*****", "doors":"*","color":"*****"}
                  ],
        "Ford": [
            {"model":"*****", "doors":"*","color":"*****"},
            {"model":"*****", "doors":"*","color":"*****"}
        ]
    }
}

什么查询会给我以下结果?

它是一个 JSON 对象(汽车),其中包含多个数组,每个数组用于不同型号的汽车。在每个数组中都会有其他类型的数据,例如门、颜色、年份……等等

我试过这段代码:

<?php 

require 'conn_pdo.php';

$conn -> setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); 

$sql = "select * from test_cars where cars in (select cars from test_cars GROUP BY cars) ORDER BY RAND()";

$stmt = $conn -> prepare($sql);
$stmt -> execute();
$row = $stmt -> fetchAll();
$json['cars'] = $row;
echo json_encode($json);

?>

但结果不是我希望的

{ "cars": 
         [ {"model":"*****", "doors":*","color":*****"},  
           {"model":"*****","doors":"*","color":*****"} ,
           {"model":"*****", "doors":"*","color":*****"}, 
           {"model":"*****", "doors":"*","color":*****"},
           .......
         ] 
}

我得到了汽车对象,其中包含 一个数组 用于所有不同型号的汽车!!

【问题讨论】:

  • 您的数据库查询无关紧要,将 php 中的结果处理成您想要的任何格式。如果您需要更多帮助,请出示您的代码
  • 不要将未格式化的代码转储到 cmets,而是更新问题,以便所有人都能阅读
  • @charlietfl 你能帮帮我吗!
  • 可以看到创建此输出的 PHP 吗?我怀疑您需要遍历您的汽车,并按制造商分组。您可以在查询中使用 GROUP BY 或在 PHP 中使用循环来做到这一点。
  • 帮助什么,代码去哪儿了?您删除了评论但没有更新问题

标签: php jquery mysql arrays json


【解决方案1】:

这是一种方法。我删除了 GROUP BY 方法,因为问题中的方法没有正确使用它 - 它通过使用 IN 查询将分组丢弃。无论如何,我认为用 PHP 很容易做到。

// It would be good to check to ensure you have a valid connection,
// and that the prepare/execute calls are successful
$sql = "SELECT * from test_cars";
$stmt = $conn->prepare($sql);
$stmt->execute();
$rows = $stmt->fetchAll();

$out = [];
foreach ($rows as $row)
{
    // Let's process by brand (since you have not shown your
    // schema, you will need to adjust column names to suit)
    $brand = $row['brand'];

    // Get other data
    $model = $row['model'];
    $doors = $row['doors'];
    $colour = $row['colour'];

    // Check to ensure the brand is an array, so that we don't
    // get an error when we push
    if (!isset($out[$brand]))
    {
        $out[$brand] = [];
    }

    // Now push the data on to the end (using [])
    $out[$brand][] = [
        'model' => $model,
        'doors' => $doors,
        'colour' => $colour,
    ];
}

$json['cars'] = $out;
echo json_encode($json);

这是未经测试的,但应该给你一个大致的想法。您应该期望必须对其进行一些调整,就像 Stack Overflow 上的所有答案一样。我使用了新的数组语法(从 PHP 5.4 开始),但如果你将它换成 array(),它将适用于早期版本。

【讨论】:

  • 感谢答案^_^ 但我得到致命错误:调用未定义函数 is_set() !!
  • 还有一件事我想问你们!!如何将随机 json 数组 (model ,door, colour) 显示到 (door,model, colour) ....ect
  • 太好了,很高兴它正在工作!值得一提的是——请不要假设这里的每个人都是“老兄”——我的昵称没有任何性别含义:-)。由于这对您有用,因此如果您接受答案,我会提出增加随机性的建议。
  • 不傻,不 - 请参阅我写的 :-)。我认为值得鼓励人们不要假设程序员总是男性。无论如何,我很高兴这对你有用 - 祝你的项目好运!
猜你喜欢
  • 2015-05-15
  • 2013-12-20
  • 2010-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多