[英]Database Row with Multiple JSON values - How to get a specific value
所以我今天的問題是,如何使用Php從具有多個JSON值的單個數據庫行中獲取特定值。
數據已按以下方式放入數據庫中:
a:7:{s:5:"cname";s:4:"enes";s:6:"cemail";s:22:"EnesDemirICT@gmail.com";s:8:"caddress";s:18:"Florisvosstraat 56";s:4:"ctel";s:12:"+31685035990";s:9:"ccomments";s:5:"fdfsf";s:15:"wppizza-gateway";s:3:"cod";s:12:"wppizza_hash";s:64:"34e24d48186596cae445d52c5b0650d20c8a7c215ef1fe323ca6f09b7f804a8d";}
因此,當然我已經有了從數據庫中獲取此代碼的代碼,如下所示。
<?php
header('content-type:application/json');
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('depits');
$select = mysql_query("SELECT * FROM `test_json`");
$rows=array();
while($row=mysql_fetch_array($select))
{
$rows[] = $row;
}
echo json_encode($rows);
?>
我將如何處理收到的數據,例如,僅獲取名稱和電子郵件的值。 如果這個問題含糊不清或根本不是一個好問題,請對此發表評論,以便為了清楚起見我可能會更好。
您的Json似乎不正確, http://jsonlint.com/
這是我的方法:
<?
$test = '{"John": {"status":"Wait"},"Jennifer":{"status":"Active"},"James": {"status":"Active", "age":56,"count":10,"progress":0.0029857,"bad":0}}';
// decode your json into associative arrays
$decoded = json_decode($test, true);
//easy to read the array
echo "<pre>";
print_r($decoded);
echo "</pre>";
// james age
echo "Age: ". $decoded['James']['age'];
?>
我想這就是您要尋找的。 要獲取所有名稱,您需要一個foreach循環
<?
$json = '{
"People":
[{
"name":"peter",
"age" :"50"
},{
"name":"phil",
"age" :"25"
},{
"name":"mike",
"age" :"23"
}]
}';
// decode your json into associative arrays
$decoded = json_decode($json, true);
// to loop through the array
foreach($decoded['People'] as $key => $value) {
echo 'Name: ' . $value['name'] . ' Age: ' . $value['age'] . '<br>';
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.