Json从表中获取价值

时间:2014-02-22 21:42:03

标签: php json

我有一个小代码来制作这个结果:

  

{"nomdupharmacie":[{"pid":"71","name":"dft","longi":"9.010505676269531","lati":"34.1575970207261","matricule":"M65203124"},{"pid":"72","name":"erezrzer","longi":"7.529407627880573","lati":"34.63767601827405","matricule":"123"},{"pid":"73","name":"qsd","longi":"8.83832462131977","lati":"35.172592315800905","matricule":"333"}],"success":1}

使用这个PHP代码:

//从产品表中获取所有产品

$result = mysql_query("SELECT *FROM nomdupharmacie") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["nomdupharmacie"] = array();
    $v = "12";
    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $product = array();
        $product["pid"] = $row["pid"];
        $product["name"] = $row["name"];
        $product["longi"] = $row["longitude"];
        $product["lati"] = $row["latitude"];

        $product["matricule"] = $row["personnel_number"];

        // push single product into final response array
        array_push($response["nomdupharmacie"], $product);
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No products found";

    // echo no users JSON
    echo json_encode($response);
}

我必须测试一下例如: $ value =“555” 如果$ value存在于matricule列中,请获取警报 所以问题是如何测试Matricule列中是否存在值?

1 个答案:

答案 0 :(得分:0)

您应该考虑使用PDO或ADOdb而不是不推荐使用的mysql_query。

唯一的选择是像这样手动比较:

1)创建一个接收数组x和值v的函数来检查

function isMatriculeInArray($x, $v)
{
    foreach($x as $z)
    {
        if($z["matricule"] == $v)
        {
            return true;
        }
    }
    return false;
}

2)检查matricule是否在那里

if(!isMatriculeInArray($response["nomdupharmacie"], $v)
{
    array_push($response["nomdupharmacie"], $product);
}