从数据库返回JSON数据

时间:2018-04-09 17:53:53

标签: php mysql laravel

到目前为止,这是我的代码:

foreach($prices as $price) {
    $data["id"] =  $price->id;
}

$result["data"] = $data;

我希望它成为这样的东西:

{
    "data": [
        {"id": 1},
        {"id": 2},
        {"id": 3}
    ]
}

3 个答案:

答案 0 :(得分:2)

image: docker:latest
services:
  - docker:dind

variables:
  SPRING_PROFILES_ACTIVE: gitlab-ci

stages:
  - build
  - package
  - deploy

maven-build:
  image: maven:3-jdk-8
  stage: build
  script: "mvn package -B"
  artifacts:
    paths:
      - target/*.jar

docker-build:
  stage: package
  script:
  - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN https://devel.priv.net
  - docker build -t https://devel.priv.net/d.p/service-discovery
  - docker push https://devel.priv.net/d.p/service-discovery

答案 1 :(得分:0)

您必须使用json_encode()来返回值

的JSON表示

json_encode

答案 2 :(得分:0)



<?php 
<br>
      $prices = ['data'=>['id'=> '1', 'orange'=>'2']];
foreach($prices as $price) {
    $data['data'] =  $price;
}

$result = $data;
echo json_encode($result)
?>
&#13;
&#13;
&#13;

输出

{&#34;数据&#34;:{&#34; id&#34;:&#34; 1&#34;,&#34;橙&#34;:&#34; 2&#34;} }