doudaotui4297 2016-07-13 12:51
浏览 47
已采纳

使用休息显示细节

using rest want to show the details about the items but there is a bug in the function code but according to me that is right but there is error in 7th line. i have two code they are as follow index.php

<?php
header("content-Type:application/json");
include("function.php");
if(!empty($_GET['name'])){

    $name = $_GET['name'];
    $price = get_price($name);

    if(empty($price))
        deliver_response(200,"book not found",NULL);
    else
        deliver_response(200,"book found",price);
}
else{
    deliver_response(400,"invalid",NULL);
}

function deliver_response($status,$status_message,$data)
{
    header("HTTP/1.1 $status $status_message");

    $response['status']=$status;
    $response['status_message']=$status_message;
    $response['data']=$data;

    $json_response=json_encode($response);
    echo $json_response;


}
?>

the function code

<?php
function get_price($find)
{
    $books = array(
        "java" => 300,
        "c"    => 250,
        "php"  => 350);
    for($books as $book => $price) {

        if($books == $find) {
            return $price;
            break;
        }
    }
}

?>

</div>
  • 写回答

3条回答 默认 最新

  • doufu8127 2016-07-13 13:01
    关注

    Use a foreach loop instead of a for loop and $book instead of $books in your if statement.

    foreach ( $books AS $book => $price ) {
        if ( $book == $find ) {
            return $price;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用