从php数组获取值并输出到JSON

时间:2014-03-24 21:44:24

标签: php arrays json web-services

我是php的新手,我正在尝试创建一个Web服务。我有一个函数从数据库中选择一些正确预编码的行(它返回22行),但是当我从index.php页面调用该函数并输出到JSON时,我只得到第一行输出。

有人能指出我正确的方向吗?

MySQL功能:

    public function getStops()
    {
        //Perform query
        $result = mysql_query("SELECT station_name FROM station WHERE line =   2") or die(mysql_error());

        //Check result
        $numRows = mysql_num_rows($result);

        if($numRows > 0)
        {
            return mysql_fetch_array($result);
        }
        else
        {
            return false;
        }
    }

index.php中调用getStops()函数的代码:

            //Check tag request
    if ($tag == "greenLineStations")
    {
        $greenStops = $db->getGreenLineStops();

        if ($greenStops != false)
        {
            //Get data and set success = 1
            $response["success"] = 1;
            foreach ($greenStops as $value)
            {
            $response["stops"]['name'] = $value;
            }
            echo json_encode($response);
        }
        else
        {
            //Set error = 1 and out error message
            $response["error"] = 1;
            $response["error_message"] = "Error occured while retreiving stations";
            echo json_encode($response);
        }
    }
    else
    {
        echo "Invalid Request";
    }
} 

JSON转储:

    {"success":1,"error":0,"stops":[{"name":{"0":"St. Stephen's Green","station_name":"St. Stephen's Green"}},{"name":{"0":"Harcourt","station_name":"Harcourt"}},{"name":{"0":"Charlemont","station_name":"Charlemont"}},{"name":{"0":"Ranelagh","station_name":"Ranelagh"}},{"name":{"0":"Beechwood","station_name":"Beechwood"}},{"name":{"0":"Cowper","station_name":"Cowper"}},{"name":{"0":"Milltown","station_name":"Milltown"}},{"name":{"0":"Windy Arbour","station_name":"Windy Arbour"}},{"name":{"0":"Dundrum","station_name":"Dundrum"}},{"name":{"0":"Balally","station_name":"Balally"}},{"name":{"0":"Kilmacud","station_name":"Kilmacud"}},{"name":{"0":"Stillorgan","station_name":"Stillorgan"}},{"name":{"0":"Sandyford","station_name":"Sandyford"}},{"name":{"0":"Central Park","station_name":"Central Park"}},{"name":{"0":"Glencairn","station_name":"Glencairn"}},{"name":{"0":"The Gallops","station_name":"The Gallops"}},{"name":{"0":"Leopardstown Valley","station_name":"Leopardstown Valley"}},{"name":{"0":"Ballyogan Wood","station_name":"Ballyogan Wood"}},{"name":{"0":"Carrickmines","station_name":"Carrickmines"}},{"name":{"0":"Laughanstown","station_name":"Laughanstown"}},{"name":{"0":"Cherrywood","station_name":"Cherrywood"}},{"name":{"0":"Brides Glen","station_name":"Brides Glen"}}]}

2 个答案:

答案 0 :(得分:2)

   if($numRows > 0)
   {
      $return = array();
      while($array = mysql_fetch_array($result)){
          $return[] = $array;
      }
      return $return;
   }

和:

        $response["success"] = 1;
        $response['stops'] = array();
        foreach ($greenStops as $value){
            $response["stops"][]['name'] = $value[0];
        }

答案 1 :(得分:0)

看起来你的index.php中的foreach循环只是一遍又一遍地重写相同的索引。根据您希望输出结构的结构,我建议将第12行更改为:

$response["stops"][]['name'] = $value;