本文介紹了php - foreach - 通過鍵名獲取具體數據的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如何從以下 foreach 代碼中通過鍵名獲取特定數據.
how to get specific data by key name from the following foreach code.
<?php
foreach ($_SESSION as $key=>$val)
echo $key." ".$val."<br/>";
?>
數組看起來像這樣想要獲取特定鍵的值.
array is look like this want to get value of specific key.
{"name":"bedroom","rent":"sale","props":"","leases":""}
我嘗試了以下代碼但不起作用
i have tried following code but doesn't work
echo "checking key sep. ".$key[rent];
如果可能,我可以通過姓名或租金等關鍵名稱訪問.
if possible i can access by key name like name or rent.
推薦答案
格式為JSON,需要先使用json_decode()
The format is in JSON , you need to decode it first by using json_decode()
這樣的東西...
$yourJSON = '{"name":"bedroom","rent":"sale","props":"","leases":""}';
$yourarray = json_decode($yourJSON,1);
然后您可以像這樣使用 foreach
構造來循環 $yourarray
.
You can then loop the $yourarray
using the foreach
construct like this.
foreach($yourarray as $key=>$val)
{
echo $key." ".$val."<br/>";
}
用于從中檢索特定密鑰..
For retrieving a specific key from it..
echo $yourarray['rent']; //"prints" sale
這篇關于php - foreach - 通過鍵名獲取具體數據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!